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

APN-Pucky / tyrant_optimize / 39674783343

21 Oct 2026 08:57PM UTC coverage: 70.508% (+0.02%) from 70.491%
39674783343

push

github

web-flow
Faster sim and climb: fewer memsets, copies and allocations; double-precision bounds (#115)

* Sim - clear the enhance/evolve skill arrays at turn end only when they were modified

* Sim - clear the four CardStatus skill arrays with one memset

* Cards - look up cards by id in a hash map instead of a tree

* Sim - take the skill targets out of the selection array instead of copying it

* Sim - mimic reads the target's skills in place instead of copying them

* Climb - compute the confidence bounds in double precision

Boost promotes double to long double by default. Where long double is a
software-emulated 128-bit type (WebAssembly) that is very slow and its
root finding fails near 100% wins: "gamma_p_inv<long double>: Root
finding evaluation exceeded 200 iterations" ends the climb. Natively the
bounds move by at most 2e-14 relative; 60 fixed-seed climbs are
unchanged.

21 of 21 new or added lines in 2 files covered. (100.0%)

1 existing line in 1 file now uncovered.

4803 of 6812 relevant lines covered (70.51%)

10162699.82 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); }
18,186,509 ✔
39
inline bool is_alive(CardStatus* c) { return (c->m_hp > 0); }
18,046,050 ✔
40
inline bool can_act(CardStatus* c) { return is_alive(c) && !c->m_jammed; }
465,530,353 ✔
41
inline bool is_active(CardStatus* c) { return can_act(c) && (c->m_delay == 0); }
388,232,116 ✔
42
inline bool is_active_next_turn(CardStatus* c) { return can_act(c) && (c->m_delay <= 1); }
53,430,302 ✔
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)));}
255,009 ✔
44
// Can be healed / repaired
45
inline bool can_be_healed(CardStatus* c) { return is_alive(c) && (c->m_hp < c->max_hp()); }
35,186 ✔
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)
55,234,236 ✔
60
{
61
    return status->description();
21,988,564 ✔
62
}
63
//------------------------------------------------------------------------------
64
template <typename CardsIter, typename Functor>
65
inline unsigned Field::make_selection_array(CardsIter first, CardsIter last, Functor f)
131,481,087 ✔
66
{
67
    this->selection_array.clear();
131,481,087 ✔
68
    for(auto c = first; c != last; ++c)
533,518,022 ✔
69
    {
70
        if (f(*c))
799,180,452 ✔
71
        {
72
            this->selection_array.push_back(*c);
204,465,243 ✔
73
        }
74
    }
75
    return(this->selection_array.size());
131,481,087 ✔
76
}
77
inline CardStatus * Field::left_assault(const CardStatus * status)
1,059,841 ✔
78
{
79
    return left_assault(status, 1);
3,993,055 ✔
80
}
81
inline CardStatus * Field::left_assault(const CardStatus * status, const unsigned n)
1,059,841 ✔
82
{
83
    auto & assaults = this->players[status->m_player]->assaults;
1,059,841 ✔
84
    if (status->m_index >= n)
1,059,841 ✔
85
    {
86
        auto left_status = &assaults[status->m_index - n];
809,360 ✔
87
        if (is_alive(left_status))
809,360 ✔
88
        {
89
            return left_status;
90
        }
91
    }
92
    return nullptr;
93
}
94
inline CardStatus * Field::right_assault(const CardStatus * status)
1,059,841 ✔
95
{
96
    return right_assault(status, 1);
3,959,962 ✔
97
}
98
inline CardStatus * Field::right_assault(const CardStatus * status, const unsigned n)
1,059,841 ✔
99
{
100
    auto & assaults = this->players[status->m_player]->assaults;
1,059,841 ✔
101
    if ((status->m_index + n) < assaults.size())
1,059,841 ✔
102
    {
103
        auto right_status = &assaults[status->m_index + n];
816,482 ✔
104
        if (is_alive(right_status))
816,482 ✔
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()
112,823,810 ✔
123
{
124
    damaged_units_to_times.clear();
225,647,620 ✔
125
}
126

127
//------------------------------------------------------------------------------
128
inline void Field::finalize_action()
111,303,121 ✔
129
{
130
    for (auto unit_it = damaged_units_to_times.begin(); unit_it != damaged_units_to_times.end(); ++ unit_it)
111,578,546 ✔
131
    {
132
        if (__builtin_expect(!unit_it->second, false))
275,425 ✔
133
        { continue; }
×
134
        CardStatus * dmg_status = unit_it->first;
275,425 ✔
135
        if (__builtin_expect(!is_alive(dmg_status), false))
275,425 ✔
136
        { continue; }
120,413 ✔
137
        unsigned barrier_base = dmg_status->skill(Skill::barrier);
155,012 ✔
138
        if (barrier_base)
155,012 ✔
139
        {
140
            unsigned protect_value = barrier_base * unit_it->second;
155,012 ✔
141
            _DEBUG_MSG(1, "%s protects itself for %u (barrier %u x %u damage taken)\n",
155,012 ✔
142
                    status_description(dmg_status).c_str(), protect_value, barrier_base, unit_it->second);
155,012 ✔
143
            dmg_status->m_protected += protect_value;
155,012 ✔
144
        }
145
    }
146
}
111,303,121 ✔
147

148
//------------------------------------------------------------------------------
149
inline unsigned CardStatus::skill_base_value(Skill::Skill skill_id) const
1,378,514,456 ✔
150
{
151
    return m_card->m_skill_value[skill_id + m_primary_skill_offset[skill_id]]
1,378,514,456 ✔
152
        + (skill_id == Skill::berserk ? m_enraged : 0)
×
153
        + (skill_id == Skill::counter ? m_entrapped : 0)
39,504,366 ✔
154
        ;
155
}
156
//------------------------------------------------------------------------------
157
inline unsigned CardStatus::skill(Skill::Skill skill_id) const
871,751,172 ✔
158
{
159
    return (is_activation_skill_with_x(skill_id)
871,751,172 ✔
160
            ? safe_minus(skill_base_value(skill_id), m_sabotaged)
161
            : skill_base_value(skill_id))
873,859,136 ✔
162
        + enhanced(skill_id);
873,859,136 ✔
163
}
164
//------------------------------------------------------------------------------
165
inline bool CardStatus::has_skill(Skill::Skill skill_id) const
504,593,449 ✔
166
{
167
    return skill_base_value(skill_id);
52,821 ✔
168
}
169
//------------------------------------------------------------------------------
170
inline unsigned CardStatus::enhanced(Skill::Skill skill_id) const
988,169,402 ✔
171
{
172
    return m_enhanced_value[skill_id + m_primary_skill_offset[skill_id]];
155,012 ✔
173
}
174
//------------------------------------------------------------------------------
175
inline unsigned CardStatus::protected_value() const
57,771,787 ✔
176
{
177
    return m_protected + m_protected_stasis;
57,771,787 ✔
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
109,649,509 ✔
187
{
188
    return (m_card->m_health + safe_minus(m_perm_health_buff, m_subdued));
105,273,804 ✔
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)
23,714,800 ✔
197
{
198
    value = remove_disease(this,value);
23,714,800 ✔
199
    return (m_hp = std::min(m_hp + value, max_hp()));
42,170,349 ✔
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)
14,514,951 ✔
209
{
210
    value = remove_disease(this,value);
14,514,951 ✔
211
    m_perm_health_buff += value;
14,514,951 ✔
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);
14,514,951 ✔
215
}
216
//------------------------------------------------------------------------------
217
inline void CardStatus::set(const Card* card)
27,468,683 ✔
218
{
219
    this->set(*card);
27,468,683 ✔
220
}
221
//------------------------------------------------------------------------------
222
inline void CardStatus::set(const Card& card)
27,468,683 ✔
223
{
224
    m_card = &card;
27,468,683 ✔
225
    m_index = 0;
27,468,683 ✔
226
    m_action_index=0;
27,468,683 ✔
227
    m_player = 0;
27,468,683 ✔
228
    m_delay = card.m_delay;
27,468,683 ✔
229
    m_hp = card.m_health;
27,468,683 ✔
230
    m_absorption = 0;
27,468,683 ✔
231
    m_step = CardStep::none;
27,468,683 ✔
232
    m_perm_health_buff = 0;
27,468,683 ✔
233
    m_perm_attack_buff = 0;
27,468,683 ✔
234
    m_temp_attack_buff = 0;
27,468,683 ✔
235
    m_corroded_rate = 0;
27,468,683 ✔
236
    m_corroded_weakened = 0;
27,468,683 ✔
237
    m_subdued = 0;
27,468,683 ✔
238
    m_enfeebled = 0;
27,468,683 ✔
239
    m_evaded = 0;
27,468,683 ✔
240
    m_inhibited = 0;
27,468,683 ✔
241
    m_sabotaged = 0;
27,468,683 ✔
242
    m_jammed = false;
27,468,683 ✔
243
    m_overloaded = false;
27,468,683 ✔
244
    m_paybacked = 0;
27,468,683 ✔
245
    m_tributed = 0;
27,468,683 ✔
246
    m_poisoned = 0;
27,468,683 ✔
247
    m_protected = 0;
27,468,683 ✔
248
    m_protected_stasis = 0;
27,468,683 ✔
249
    m_enraged = 0;
27,468,683 ✔
250
    m_entrapped = 0;
27,468,683 ✔
251
    m_marked = 0;
27,468,683 ✔
252
    m_diseased = 0;
27,468,683 ✔
253
    m_rush_attempted = false;
27,468,683 ✔
254
    m_sundered = false;
27,468,683 ✔
255
    //APN
256
    m_summoned = false;
27,468,683 ✔
257
    m_skills_modified = false;
27,468,683 ✔
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)
27,468,683 ✔
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));
27,468,683 ✔
264
}
27,468,683 ✔
265
//------------------------------------------------------------------------------
266
/**
267
 * @brief Calculate the attack power of the CardStatus.
268
 * 
269
 * @return unsigned 
270
 */
271
inline unsigned CardStatus::attack_power() const
205,880,049 ✔
272
{
273
    signed attack = calc_attack_power();
205,880,049 ✔
274
    if(__builtin_expect(attack <0,false))
205,880,049 ✔
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);
205,880,049 ✔
279
    return (unsigned)attack;
205,880,049 ✔
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
206,877,214 ✔
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."
206,877,214 ✔
295
    {
296
        return
206,877,214 ✔
297
        (signed)safe_minus(
413,754,428 ✔
298
                m_card->m_attack + safe_minus(m_perm_attack_buff, m_subdued)+ m_temp_attack_buff,
206,877,214 ✔
299
                m_corroded_weakened
206,877,214 ✔
300
                );
206,877,214 ✔
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)
108,213 ✔
320
{
321
    const auto cardIter = cards.cards_by_id.find(card_id);
108,213 ✔
322
    if (cardIter == cards.cards_by_id.end()) { return "UnknownCard.id[" + tuo::to_string(card_id) + "]"; }
108,213 ✔
323
    return cardIter->second->m_name;
216,426 ✔
324
}
325
//------------------------------------------------------------------------------
326
std::string card_description(const Cards& cards, const Card* c)
2,514,301 ✔
327
{
328
    std::string desc;
2,514,301 ✔
329
    desc = c->m_name;
2,514,301 ✔
330
    switch(c->m_type)
2,514,301 ✔
331
    {
332
        case CardType::assault:
2,076,658 ✔
333
            desc += ": " + tuo::to_string(c->m_attack) + "/" + tuo::to_string(c->m_health) + "/" + tuo::to_string(c->m_delay);
10,383,290 ✔
334
            break;
2,076,658 ✔
335
        case CardType::structure:
437,077 ✔
336
            desc += ": " + tuo::to_string(c->m_health) + "/" + tuo::to_string(c->m_delay);
1,748,308 ✔
337
            break;
437,077 ✔
338
        case CardType::commander:
566 ✔
339
            desc += ": hp:" + tuo::to_string(c->m_health);
1,698 ✔
340
            break;
566 ✔
341
        case CardType::num_cardtypes:
×
342
            assert(false);
×
343
            break;
344
    }
345
    if (c->m_rarity >= 4) { desc += " " + rarity_names[c->m_rarity]; }
4,219,870 ✔
346
    if (c->m_faction != allfactions) { desc += " " + faction_names[c->m_faction]; }
5,028,602 ✔
347
    for (auto& skill: c->m_skills_on_play) { desc += ", " + skill_description(cards, skill, Skill::Trigger::play); }
2,647,437 ✔
348
    for (auto& skill: c->m_skills) { desc += ", " + skill_description(cards, skill, Skill::Trigger::activate); }
16,630,839 ✔
349
    //APN
350
    for (auto& skill: c->m_skills_on_attacked) { desc += ", " + skill_description(cards, skill, Skill::Trigger::attacked); }
3,078,353 ✔
351
    for (auto& skill: c->m_skills_on_death) { desc += ", " + skill_description(cards, skill, Skill::Trigger::death); }
2,689,183 ✔
352
    return desc;
2,514,301 ✔
353
}
×
354
//------------------------------------------------------------------------------
355
std::string CardStatus::description() const
55,234,236 ✔
356
{
357
    std::string desc = "P" + tuo::to_string(m_player) + " ";
165,702,708 ✔
358
    switch(m_card->m_type)
55,234,236 ✔
359
    {
360
        case CardType::commander: desc += "Commander "; break;
17,035,308 ✔
361
        case CardType::assault: desc += "Assault " + tuo::to_string(m_index) + " "; break;
127,218,968 ✔
362
        case CardType::structure: desc += "Structure " + tuo::to_string(m_index) + " "; break;
25,576,744 ✔
363
        case CardType::num_cardtypes: assert(false); break;
×
364
    }
365
    desc += "[" + m_card->m_name;
110,468,472 ✔
366
    switch (m_card->m_type)
55,234,236 ✔
367
    {
368
        case CardType::assault:
31,804,742 ✔
369
            desc += " att:[[" + tuo::to_string(m_card->m_attack) + "(base)";
127,218,968 ✔
370
            if (m_perm_attack_buff)
31,804,742 ✔
371
            {
372
                desc += "+[" + tuo::to_string(m_perm_attack_buff) + "(perm)";
28,914,756 ✔
373
                if (m_subdued) { desc += "-" + tuo::to_string(m_subdued) + "(subd)"; }
8,479,599 ✔
374
                desc += "]";
7,228,689 ✔
375
            }
376
            if (m_corroded_weakened) { desc += "-" + tuo::to_string(m_corroded_weakened) + "(corr)"; }
33,205,301 ✔
377
            desc += "]";
31,804,742 ✔
378
            if (m_temp_attack_buff) { desc += (m_temp_attack_buff > 0 ? "+" : "") + tuo::to_string(m_temp_attack_buff) + "(temp)"; }
48,888,551 ✔
379
            desc += "]=" + tuo::to_string(attack_power());
95,414,226 ✔
380
        case CardType::structure:
55,234,236 ✔
381
        case CardType::commander:
55,234,236 ✔
382
            desc += " hp:" + tuo::to_string(m_hp);
165,702,708 ✔
383
            if(m_absorption)desc += " absorb:" + tuo::to_string(m_absorption);
60,957,360 ✔
384
            break;
385
        case CardType::num_cardtypes:
×
386
            assert(false);
×
387
            break;
388
    }
389
    if (m_delay) { desc += " cd:" + tuo::to_string(m_delay); }
79,887,638 ✔
390
    // Status w/o value
391
    if (m_jammed) { desc += ", jammed"; }
55,234,236 ✔
392
    if (m_overloaded) { desc += ", overloaded"; }
55,234,236 ✔
393
    if (m_sundered) { desc += ", sundered"; }
55,234,236 ✔
394
    if (m_summoned) { desc+= ", summoned"; }
55,234,236 ✔
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) + ")"; }
57,235,560 ✔
397
    if (m_subdued) { desc += ", subdued " + tuo::to_string(m_subdued); }
57,356,244 ✔
398
    if (m_enfeebled) { desc += ", enfeebled " + tuo::to_string(m_enfeebled); }
60,508,188 ✔
399
    if (m_inhibited) { desc += ", inhibited " + tuo::to_string(m_inhibited); }
58,250,552 ✔
400
    if (m_sabotaged) { desc += ", sabotaged " + tuo::to_string(m_sabotaged); }
58,431,036 ✔
401
    if (m_poisoned) { desc += ", poisoned " + tuo::to_string(m_poisoned); }
57,020,734 ✔
402
    if (m_protected) { desc += ", protected " + tuo::to_string(m_protected); }
76,270,218 ✔
403
    if (m_protected_stasis) { desc += ", stasis " + tuo::to_string(m_protected_stasis); }
60,047,408 ✔
404
    if (m_enraged) { desc += ", enraged " + tuo::to_string(m_enraged); }
61,388,084 ✔
405
    if (m_entrapped) { desc += ", entrapped " + tuo::to_string(m_entrapped); }
74,868,172 ✔
406
    if (m_marked) { desc += ", marked " + tuo::to_string(m_marked); }
55,539,738 ✔
407
    if (m_diseased) { desc += ", diseased " + tuo::to_string(m_diseased); }
56,329,624 ✔
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};
55,234,236 ✔
411
    for (const Skill::Trigger& trig: s_triggers)
276,171,180 ✔
412
    {
413
        std::vector<SkillSpec> card_skills(
220,936,944 ✔
414
                (trig == Skill::Trigger::play) ? m_card->m_skills_on_play :
220,936,944 ✔
415
                (trig == Skill::Trigger::activate) ? m_card->m_skills :
165,702,708 ✔
416
                //APN
417
                (trig == Skill::Trigger::attacked) ? m_card->m_skills_on_attacked :
110,468,472 ✔
418
                (trig == Skill::Trigger::death) ? m_card->m_skills_on_death :
55,234,236 ✔
419
                std::vector<SkillSpec>());
220,936,944 ✔
420

421
        // emulate Berserk/Counter by status Enraged/Entrapped unless such skills exist (only for normal skill triggering)
422
        if (trig == Skill::Trigger::activate)
220,936,944 ✔
423
        {
424
            if (m_enraged && !std::count_if(card_skills.begin(), card_skills.end(), [](const SkillSpec ss) { return (ss.id == Skill::berserk); }))
58,311,160 ✔
425
            {
426
                SkillSpec ss{Skill::berserk, m_enraged, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
3,036,113 ✔
427
                card_skills.emplace_back(ss);
3,036,113 ✔
428
            }
429
            if (m_entrapped && !std::count_if(card_skills.begin(), card_skills.end(), [](const SkillSpec ss) { return (ss.id == Skill::counter); }))
65,051,204 ✔
430
            {
431
                SkillSpec ss{Skill::counter, m_entrapped, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
9,611,123 ✔
432
                card_skills.emplace_back(ss);
9,611,123 ✔
433
            }
434
        }
435
        for (const auto& ss : card_skills)
398,553,248 ✔
436
        {
437
            std::string skill_desc;
177,616,304 ✔
438
            if (m_evolved_skill_offset[ss.id]) { skill_desc += "->" + skill_names[ss.id + m_evolved_skill_offset[ss.id]]; }
178,014,880 ✔
439
            if (m_enhanced_value[ss.id]) { skill_desc += " +" + tuo::to_string(m_enhanced_value[ss.id]); }
180,484,974 ✔
440
            if (!skill_desc.empty())
177,616,304 ✔
441
            {
442
                desc += ", " + (
3,665,822 ✔
443
                        (trig == Skill::Trigger::play) ? "(On Play)" :
3,665,822 ✔
444
                        (trig == Skill::Trigger::attacked) ? "(On Attacked)" :
1,832,911 ✔
445
                        (trig == Skill::Trigger::death) ? "(On Death)" :
1,832,911 ✔
446
                        std::string("")) + skill_names[ss.id] + skill_desc;
9,164,555 ✔
447
            }
448
        }
177,616,304 ✔
449
    }
220,936,944 ✔
450
    return desc + "]";
55,234,236 ✔
451
}
55,234,236 ✔
452
//------------------------------------------------------------------------------
453
void Hand::reset(std::mt19937& re)
3,083,098 ✔
454
{
455
    assaults.reset();
3,083,098 ✔
456
    structures.reset();
3,083,098 ✔
457
    deck->shuffle(re);
3,083,098 ✔
458
    commander.set(deck->shuffled_commander);
3,083,098 ✔
459
    total_cards_destroyed = 0;
3,083,098 ✔
460
    total_nonsummon_cards_destroyed = 0;
3,083,098 ✔
461
    if (commander.skill(Skill::stasis))
3,083,098 ✔
462
    {
463
        stasis_faction_bitmap |= (1u << commander.m_card->m_faction);
×
464
    }
465
}
3,083,098 ✔
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)
54,013,967 ✔
475
{
476
    return((player + 1) % 2);
54,013,967 ✔
477
}
478

479
//------------------------------------------------------------------------------
480
SkillSpec apply_evolve(const SkillSpec& s, signed offset)
337,205 ✔
481
{
482
    SkillSpec evolved_s = s;
337,205 ✔
483
    evolved_s.id = static_cast<Skill::Skill>(evolved_s.id + offset);
337,205 ✔
484
    return(evolved_s);
337,205 ✔
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)
188,707 ✔
497
{
498
    SkillSpec sabotaged_s = s;
188,707 ✔
499
    sabotaged_s.x -= std::min(sabotaged_s.x, sabotaged_value);
×
500
    return(sabotaged_s);
188,707 ✔
501
}
502

503
//------------------------------------------------------------------------------
504
inline void resolve_scavenge(Storage<CardStatus>& store)
41,979,108 ✔
505
{
506
    for(auto status : store.m_indirect)
158,585,002 ✔
507
    {
508
        if(!is_alive(status))continue;
116,605,894 ✔
509
        unsigned scavenge_value = status->skill(Skill::scavenge);
101,942,257 ✔
510
        if(!scavenge_value)continue;
101,942,257 ✔
511

512
        _DEBUG_MSG(1, "%s activates Scavenge %u\n",
7,434,814 ✔
513
                status_description(status).c_str(), scavenge_value);
7,434,814 ✔
514
        status->ext_hp(scavenge_value);
7,434,814 ✔
515
    }
516
}
41,979,108 ✔
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)
130,860,853 ✔
525
{
526
    if (fd->killed_units.empty())
130,860,853 ✔
527
        return;
528
    bool skip_all_except_summon = fd->fixes[Fix::death_from_bge] && (fd->current_phase == Field::bge_phase);
10,362,215 ✔
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;
10,362,215 ✔
534
    unsigned stacked_poison_value = 0;
10,362,215 ✔
535
    unsigned last_index = 99999;
10,362,215 ✔
536
    CardStatus* left_virulence_victim = nullptr;
10,362,215 ✔
537
    for (auto status: fd->killed_units)
20,856,992 ✔
538
    {
539
        // Skill: Scavenge
540
        // Any unit dies => perm-hp-buff
541
        if (__builtin_expect(!skip_all_except_summon, true))
10,494,777 ✔
542
        {
543
            resolve_scavenge(fd->players[0]->assaults);
10,494,777 ✔
544
            resolve_scavenge(fd->players[1]->assaults);
10,494,777 ✔
545
            resolve_scavenge(fd->players[0]->structures);
10,494,777 ✔
546
            resolve_scavenge(fd->players[1]->structures);
10,494,777 ✔
547
        }
548

549
        if ((status->m_card->m_type == CardType::assault) && (!skip_all_except_summon))
10,494,777 ✔
550
        {
551
            // Skill: Avenge
552
            const unsigned host_idx = status->m_index;
9,533,397 ✔
553
            unsigned from_idx, till_idx;
9,533,397 ✔
554
            //scan all assaults for Avenge
555
            from_idx = 0;
9,533,397 ✔
556
            till_idx = assaults.size() - 1;
9,533,397 ✔
557
            for (; from_idx <= till_idx; ++ from_idx)
37,182,589 ✔
558
            {
559
                if (from_idx == host_idx) { continue; }
27,649,192 ✔
560
                CardStatus* adj_status = &assaults[from_idx];
18,115,795 ✔
561
                if (!is_alive(adj_status)) { continue; }
18,115,795 ✔
562
                unsigned avenge_value = adj_status->skill(Skill::avenge);
15,827,499 ✔
563
                if (!avenge_value) { continue; }
15,827,499 ✔
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,438,690 ✔
568
                {
569
                    avenge_value = (avenge_value + 1) / 2;
1,866,921 ✔
570
                }
571
                _DEBUG_MSG(1, "%s activates %sAvenge %u\n",
3,595,542 ✔
572
                        status_description(adj_status).c_str(),
573
                        (std::abs((signed)from_idx - (signed)host_idx) > 1 ? "Half-" : ""),
574
                         avenge_value);
3,438,690 ✔
575
                if (!adj_status->m_sundered)
3,438,690 ✔
576
                { adj_status->m_perm_attack_buff += avenge_value; }
3,209,800 ✔
577
                adj_status->ext_hp(avenge_value);
3,438,690 ✔
578
            }
579

580
            // Passive BGE: Virulence
581
            if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::virulence], false))
9,533,397 ✔
582
            {
583
                if (status->m_index != last_index + 1)
34,546 ✔
584
                {
585
                    stacked_poison_value = 0;
34,161 ✔
586
                    left_virulence_victim = nullptr;
34,161 ✔
587
                    if (status->m_index > 0)
34,161 ✔
588
                    {
589
                        auto left_status = &assaults[status->m_index - 1];
7,160 ✔
590
                        if (is_alive(left_status))
7,160 ✔
591
                        {
592
                            left_virulence_victim = left_status;
34,546 ✔
593
                        }
594
                    }
595
                }
596
                if (status->m_poisoned > 0)
34,546 ✔
597
                {
598
                    if (left_virulence_victim != nullptr)
1,563 ✔
599
                    {
600
                        _DEBUG_MSG(1, "Virulence: %s spreads left poison +%u to %s\n",
126 ✔
601
                                status_description(status).c_str(), status->m_poisoned,
602
                                status_description(left_virulence_victim).c_str());
126 ✔
603
                        left_virulence_victim->m_poisoned += status->m_poisoned;
126 ✔
604
                    }
605
                    stacked_poison_value += status->m_poisoned;
1,563 ✔
606
                    _DEBUG_MSG(1, "Virulence: %s spreads right poison +%u = %u\n",
1,563 ✔
607
                            status_description(status).c_str(), status->m_poisoned, stacked_poison_value);
608
                }
609
                if (status->m_index + 1 < assaults.size())
34,546 ✔
610
                {
611
                    auto right_status = &assaults[status->m_index + 1];
11,190 ✔
612
                    if (is_alive(right_status))
11,190 ✔
613
                    {
614
                        _DEBUG_MSG(1, "Virulence: spreads stacked poison +%u to %s\n",
10,370 ✔
615
                                stacked_poison_value, status_description(right_status).c_str());
10,370 ✔
616
                        right_status->m_poisoned += stacked_poison_value;
10,370 ✔
617
                    }
618
                }
619
                last_index = status->m_index;
34,546 ✔
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))
10,494,777 ✔
626
        {
627
            if (fd->bg_effects[fd->tapi][PassiveBGE::revenge] < 0)
73,098 ✔
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,098 ✔
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,098 ✔
631
            CardStatus* commander = &fd->players[status->m_player]->commander;
73,098 ✔
632
            _DEBUG_MSG(2, "Revenge: Preparing (head) skills  %s and %s\n",
73,098 ✔
633
                    skill_description(fd->cards, ss_heal).c_str(),
634
                    skill_description(fd->cards, ss_rally).c_str());
73,098 ✔
635
            fd->skill_queue.emplace(fd->skill_queue.begin()+0, commander, ss_heal);
73,098 ✔
636
            fd->skill_queue.emplace(fd->skill_queue.begin()+1, commander, ss_rally); // +1: keep ss_heal at first place
73,098 ✔
637
        }
638

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

658
//------------------------------------------------------------------------------
659
void(*skill_table[Skill::num_skills])(Field*, CardStatus* src, const SkillSpec&);
660
void resolve_skill(Field* fd)
196,130,339 ✔
661
{
662
    while (!fd->skill_queue.empty())
315,495,556 ✔
663
    {
664
        auto skill_instance(fd->skill_queue.front());
119,365,217 ✔
665
        auto& status(std::get<0>(skill_instance));
119,365,217 ✔
666
        const auto& ss(std::get<1>(skill_instance));
119,365,217 ✔
667
        fd->skill_queue.pop_front();
119,365,217 ✔
668
        if (__builtin_expect(status->m_card->m_skill_trigger[ss.id] == Skill::Trigger::activate, true))
119,365,217 ✔
669
        {
670
            if (!is_alive(status))
114,941,974 ✔
671
            {
672
                _DEBUG_MSG(2, "%s failed to %s because it is dead.\n",
3,942 ✔
673
                        status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
3,942 ✔
674
                continue;
3,265,674 ✔
675
            }
3,942 ✔
676
            if (status->m_jammed)
114,938,032 ✔
677
            {
678
                _DEBUG_MSG(2, "%s failed to %s because it is Jammed.\n",
1,531 ✔
679
                        status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
1,531 ✔
680
                continue;
1,531 ✔
681
            }
1,531 ✔
682
        }
683

684
        // is summon? (non-activation skill)
685
        if (ss.id == Skill::summon)
119,359,744 ✔
686
        {
687
            check_and_perform_summon(fd, status);
3,147,847 ✔
688
            continue;
3,147,847 ✔
689
        }
690
        _DEBUG_ASSERT(is_activation_skill(ss.id) || ss.id == Skill::enhance); // enhance is no trigger, but  queues the skill
116,304,599 ✔
691

692
        SkillSpec modified_s = ss;
116,211,897 ✔
693

694
        // apply evolve
695
        signed evolved_offset = status->m_evolved_skill_offset[modified_s.id];
116,211,897 ✔
696
        if (evolved_offset != 0)
116,211,897 ✔
697
        { modified_s = apply_evolve(modified_s, evolved_offset); }
337,205 ✔
698

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

704
        // apply enhance
705
        unsigned enhanced_value = status->enhanced(modified_s.id);
116,211,897 ✔
706
        if (enhanced_value > 0)
116,211,897 ✔
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)
116,211,897 ✔
711
        {
712
            _DEBUG_MSG(2, "%s failed to %s because its X value is zeroed (sabotaged).\n",
112,354 ✔
713
                    status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
112,354 ✔
714
            continue;
112,354 ✔
715
        }
112,354 ✔
716
        else { skill_table[modified_s.id](fd, status, modified_s); }
116,099,543 ✔
717
    }
718
}
196,130,339 ✔
719

720
void apply_corrosion(CardStatus * status)
23,867,123 ✔
721
{
722
      if (status->m_corroded_rate)
23,867,123 ✔
723
      {
724
        unsigned v = std::min(status->m_corroded_rate, status->attack_power());
105,632 ✔
725
        unsigned corrosion = std::min(v, status->m_card->m_attack
211,264 ✔
726
                + status->m_perm_attack_buff - status->m_corroded_weakened);
105,632 ✔
727
        _DEBUG_MSG(1, "%s loses Attack by %u (+corrosion %u).\n", status_description(status).c_str(), v, corrosion);
105,632 ✔
728
        status->m_corroded_weakened += corrosion;
105,632 ✔
729
      }
730
}
23,867,123 ✔
731
void remove_corrosion(CardStatus * status)
2,931,698 ✔
732
{
733
       if (status->m_corroded_rate)
2,931,698 ✔
734
       {
735
           _DEBUG_MSG(1, "%s loses Status corroded.\n", status_description(status).c_str());
4,419 ✔
736
           status->m_corroded_rate = 0;
4,419 ✔
737
           status->m_corroded_weakened = 0;
4,419 ✔
738
       }
739
}
2,931,698 ✔
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)
66,414,809 ✔
752
{
753
    _DEBUG_ASSERT(status);
66,414,809 ✔
754
    unsigned num_actions(1);
755
    for (unsigned action_index(0); action_index < num_actions; ++ action_index)
137,358,401 ✔
756
    {
757
        status->m_action_index = action_index;
72,464,281 ✔
758
        fd->prepare_action();
72,464,281 ✔
759
        _DEBUG_ASSERT(fd->skill_queue.size() == 0);
72,464,281 ✔
760
        for (auto & ss: skills)
279,764,672 ✔
761
        {
762
            if (!is_activation_skill(ss.id)) { continue; }
298,914,300 ✔
763
            if (status->m_skill_cd[ss.id] > 0) { continue; }
115,686,482 ✔
764
            _DEBUG_MSG(2, "Evaluating %s skill %s\n",
114,703,076 ✔
765
                    status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
766
            fd->skill_queue.emplace_back(status, ss);
114,703,076 ✔
767
            resolve_skill(fd);
114,703,076 ✔
768
        }
769
        if (type == CardType::assault)
770
        {
771
            // Attack
772
            if (can_act(status))
28,319,510 ✔
773
            {
774
                if (attack_phase(fd))
28,131,214 ✔
775
                {
776
                    *attacked = true;
25,387,812 ✔
777
                    if (__builtin_expect(fd->end, false)) { break; }
25,387,812 ✔
778
                    //Apply corrosion
779
                    apply_corrosion(status);
23,867,123 ✔
780
                }
781
                else
782
                {
783
                  // Remove Corrosion
784
                  remove_corrosion(status);
2,743,402 ✔
785
                }
786
            }
787
            else
788
            {
789
                _DEBUG_MSG(2, "%s cannot take attack.\n", status_description(status).c_str());
188,296 ✔
790
                // Remove Corrosion
791
                remove_corrosion(status);
188,296 ✔
792
            }
793
        }
794
        fd->finalize_action();
70,943,592 ✔
795
        // Flurry
796
        if (can_act(status) && status->has_skill(Skill::flurry) && (status->m_skill_cd[Skill::flurry] == 0))
143,712,391 ✔
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,705,399 ✔
805
                    status_description(status).c_str(), status->skill_base_value(Skill::flurry));
806
            num_actions += status->skill_base_value(Skill::flurry);
2,574,494 ✔
807
            for (const auto & ss : skills)
10,294,774 ✔
808
            {
809
                Skill::Skill evolved_skill_id = static_cast<Skill::Skill>(ss.id + status->m_evolved_skill_offset[ss.id]);
7,720,280 ✔
810
                if (evolved_skill_id == Skill::flurry)
7,720,280 ✔
811
                {
812
                    status->m_skill_cd[ss.id] = ss.c;
2,574,494 ✔
813
                }
814
            }
815
        }
816
    }
817
}
66,414,809 ✔
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_) :
24,385,585 ✔
829
        card{card_},
24,385,585 ✔
830
        fd{fd_},
24,385,585 ✔
831
        status{nullptr},
24,385,585 ✔
832
        storage{nullptr},
24,385,585 ✔
833
        actor_index{ai_},
24,385,585 ✔
834
        actor_status{as_}
24,385,585 ✔
835
    {}
836

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

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

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

865
            //refresh/init absorb
866
            if(status->has_skill(Skill::absorb))
24,385,585 ✔
867
            {
868
                status->m_absorption = status->skill_base_value(Skill::absorb);
1,409,503 ✔
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)
94,125,037 ✔
875
            {
876
                if (status_i == status || !is_alive(status_i)) { continue; } // except itself
69,739,452 ✔
877
                //std::cout << status_description(status_i).c_str();
878
                _DEBUG_ASSERT(is_alive(status_i));
50,204,534 ✔
879
                if (bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction))
50,204,534 ✔
880
                {
881
                    ++ same_faction_cards_count;
20,380,563 ✔
882
                    unsigned allegiance_value = status_i->skill(Skill::allegiance);
20,380,563 ✔
883
                    if (__builtin_expect(allegiance_value, false) && !status->m_summoned)
20,380,563 ✔
884
                    {
885
                        _DEBUG_MSG(1, "%s activates Allegiance %u\n", status_description(status_i).c_str(), allegiance_value);
1,252,641 ✔
886
                        if (! status_i->m_sundered)
1,164,625 ✔
887
                        { status_i->m_perm_attack_buff += allegiance_value; }
1,122,404 ✔
888
                        status_i->ext_hp(allegiance_value);
1,164,625 ✔
889
                    }
890
                }
891
                if (__builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::coldsleep], false)
50,204,534 ✔
892
                        && status_i->m_protected_stasis && can_be_healed(status_i))
119,943,986 ✔
893
                {
894
                    unsigned bge_value = (status_i->m_protected_stasis + 1) / 2;
2,211 ✔
895
                    _DEBUG_MSG(1, "Cold Sleep: %s heals itself for %u\n", status_description(status_i).c_str(), bge_value);
6,633 ✔
896
                    status_i->add_hp(bge_value);
2,211 ✔
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))
24,385,585 ✔
903
            {
904
                played_faction_mask = (1u << card->m_faction);
24,320,718 ✔
905
                // do played card have stasis? mark this faction for stasis check
906
                if (__builtin_expect(status->skill(Skill::stasis), false)
24,320,718 ✔
907
                        || __builtin_expect(fd->bg_effects[status->m_player][PassiveBGE::temporalbacklash] && status->skill(Skill::counter), false))
24,320,718 ✔
908
                {
909
                    fd->players[status->m_player]->stasis_faction_bitmap |= played_faction_mask;
4,405,934 ✔
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)
24,385,585 ✔
916
                    && ((allegiance_value = status->skill(Skill::allegiance)) > 0))
24,385,585 ✔
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,117 ✔
920
                {
921
                    if ((status_i->m_card->m_category == CardCategory::normal)
901 ✔
922
                            && (bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)))
901 ✔
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,216 ✔
930
                {
931
                    unsigned bge_value = allegiance_value * same_faction_cards_count;
424 ✔
932
                    _DEBUG_MSG(1, "Oath of Loyalty: %s activates Allegiance %u x %u = %u\n",
848 ✔
933
                            status_description(status).c_str(), allegiance_value, same_faction_cards_count, bge_value);
934
                    status->m_perm_attack_buff += bge_value;
424 ✔
935
                    status->ext_hp(bge_value);
424 ✔
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)
22,708,562 ✔
943
                    && __builtin_expect(bge_megamorphosis || (fd->players[status->m_player]->stasis_faction_bitmap & played_faction_mask), false))
42,303,608 ✔
944
            {
945
                unsigned stacked_stasis = (bge_megamorphosis || (fd->players[status->m_player]->commander.m_card->m_faction == card->m_faction))
8,598,512 ✔
946
                    ? fd->players[status->m_player]->commander.skill(Skill::stasis)
16,052,645 ✔
947
                    : 0u;
948
#ifndef NDEBUG
949
                if (stacked_stasis > 0)
7,454,133 ✔
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,823,898 ✔
957
                {
958
                    if ((bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)) && is_alive(status_i))
7,182,293 ✔
959
                    {
960
                        stacked_stasis += status_i->skill(Skill::stasis);
2,110,782 ✔
961
#ifndef NDEBUG
962
                        if (status_i->skill(Skill::stasis) > 0)
2,110,782 ✔
963
                        {
964
                            _DEBUG_MSG(2, "+ Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
7,182,293 ✔
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,128,342 ✔
972
                {
973
                    if ((bge_megamorphosis || (status_i->m_card->m_faction == card->m_faction)) && is_alive(status_i))
24,486,737 ✔
974
                    {
975
                        stacked_stasis += status_i->skill(Skill::stasis);
19,086,297 ✔
976
#ifndef NDEBUG
977
                        if (status_i->skill(Skill::stasis) > 0)
19,086,297 ✔
978
                        {
979
                            _DEBUG_MSG(2, "+ Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
8,508,572 ✔
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))
19,086,297 ✔
985
                        {
986
                            stacked_stasis += (status_i->skill(Skill::counter) + 1) / 2;
3,017 ✔
987
#ifndef NDEBUG
988
                            _DEBUG_MSG(2, "Temporal Backlash: + Stasis [%s]: stacks +%u stasis protection from %s (total stacked: %u)\n",
24,489,754 ✔
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,641,605 ✔
996
#ifndef NDEBUG
997
                if (stacked_stasis > 0)
8,641,605 ✔
998
                {
999
                    _DEBUG_MSG(1, "%s gains %u stasis protection\n",
7,053,581 ✔
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,641,605 ✔
1005
                {
1006
                    fd->players[status->m_player]->stasis_faction_bitmap &= ~played_faction_mask;
1,822,294 ✔
1007
                    _DEBUG_MSG(1, "- Stasis [%s]: no more units with stasis from %s\n",
1,831,675 ✔
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)
24,385,585 ✔
1014
                    && !summoned && card->m_category == CardCategory::normal
64,675 ✔
1015
                    && fd->players[status->m_player]->commander.m_card->m_faction == card->m_faction)
24,445,449 ✔
1016
            {
1017
                unsigned devotion_percent = fd->bg_effects[status->m_player][PassiveBGE::devotion];
9,383 ✔
1018
                unsigned bge_buff = (card->m_health*devotion_percent+99)/100;
9,383 ✔
1019
                _DEBUG_MSG(1, "Devotion %s: Gains %u HP\n",
18,766 ✔
1020
                        status_description(status).c_str(), bge_buff);
1021
                status->ext_hp(bge_buff); // <bge_value>% bonus health (rounded up)
9,383 ✔
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))
24,385,585 ✔
1028
            {
1029
                for (const auto& ss: card->m_skills_on_play)
28,205,559 ✔
1030
                {
1031
                    _DEBUG_MSG(2, "On Play %s: Preparing (tail) skill %s\n",
3,819,974 ✔
1032
                            status_description(status).c_str(), skill_description(fd->cards, ss).c_str());
1033
                    fd->skill_queue.emplace_back(status, ss);
3,819,974 ✔
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;
24,385,585 ✔
1042
        }
1043

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

1049
    template <enum CardType::CardType type>
1050
        void placeCard(bool summoned)
24,385,585 ✔
1051
        {
1052
            status = &storage->add_back();
24,385,585 ✔
1053
            status->set(card);
24,385,585 ✔
1054
            status->m_index = storage->size() - 1;
24,385,585 ✔
1055
            status->m_player = actor_index;
24,385,585 ✔
1056
            status->m_field = fd;
24,385,585 ✔
1057
            status->m_summoned = summoned;
24,385,585 ✔
1058

1059
            // reduce delay for summoned card by tower (structure) for normal (non-triggered) summon skill
1060
            if (summoned && __builtin_expect(fd->fixes[Fix::reduce_delay_summoned_by_tower], true)
24,385,585 ✔
1061
                && actor_status->m_card->m_skill_trigger[Skill::summon] == Skill::Trigger::activate
3,533,698 ✔
1062
                && actor_status->m_card->m_type == CardType::structure
389,210 ✔
1063
                && status->m_delay > 0)
8,369 ✔
1064
            {
1065
                --status->m_delay;
8,369 ✔
1066

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

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

1098
// Check if a skill actually proc'ed.
1099
    template<Skill::Skill skill_id>
1100
inline bool skill_check(Field* fd, CardStatus* c, CardStatus* ref)
259,871,392 ✔
1101
{
1102
    return is_defensive_skill(skill_id) || is_alive(c);
2,979,745 ✔
1103
}
1104

1105
    template<>
1106
inline bool skill_check<Skill::heal>(Field* fd, CardStatus* c, CardStatus* ref)
79,949,181 ✔
1107
{
1108
    return can_be_healed(c);
38,611 ✔
1109
}
1110

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

1117
    template<>
1118
inline bool skill_check<Skill::rally>(Field* fd, CardStatus* c, CardStatus* ref)
44,037,856 ✔
1119
{
1120
    return !c->m_sundered;
44,037,856 ✔
1121
}
1122

1123
    template<>
1124
inline bool skill_check<Skill::overload>(Field* fd, CardStatus* c, CardStatus* ref)
953,912 ✔
1125
{
1126
    return is_active(c) && !c->m_overloaded && !has_attacked(c);
751,774 ✔
1127
}
1128

1129
    template<>
1130
inline bool skill_check<Skill::jam>(Field* fd, CardStatus* c, CardStatus* ref)
12,283,718 ✔
1131
{
1132
    if(__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::ironwill], false) && c->has_skill(Skill::armor))return false;
12,283,718 ✔
1133
    // active player performs Jam
1134
    if (fd->tapi == ref->m_player)
12,281,532 ✔
1135
    { return is_active_next_turn(c); }
12,253,349 ✔
1136

1137

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

1142
    template<>
1143
inline bool skill_check<Skill::leech>(Field* fd, CardStatus* c, CardStatus* ref)
47,256 ✔
1144
{
1145
    return can_be_healed(c) || (fd->fixes[Fix::leech_increase_max_hp] && is_alive(c));
43,251 ✔
1146
}
1147

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

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

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

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

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

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

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

1190
    template<>
1191
inline bool skill_check<Skill::disease>(Field* fd, CardStatus* c, CardStatus* ref)
258,978 ✔
1192
{
1193
    return is_alive(c) && (ref->m_card->m_type == CardType::assault);
258,978 ✔
1194
}
1195
    template<>
1196
inline bool skill_check<Skill::inhibit>(Field* fd, CardStatus* c, CardStatus* ref)
3,223,528 ✔
1197
{
1198
    return is_alive(c) && (ref->m_card->m_type == CardType::assault);
3,223,528 ✔
1199
}
1200
    template<>
1201
inline bool skill_check<Skill::sabotage>(Field* fd, CardStatus* c, CardStatus* ref)
840,459 ✔
1202
{
1203
    return is_alive(c) && (ref->m_card->m_type == CardType::assault);
840,459 ✔
1204
}
1205
inline unsigned remove_disease(CardStatus* status, unsigned heal)
38,229,751 ✔
1206
{
1207
    unsigned remaining_heal(heal);
38,229,751 ✔
1208
    if(__builtin_expect(status->m_diseased == 0,true))
38,229,751 ✔
1209
    {
1210
        //skip
1211
    }
1212
    else if (heal > status->m_diseased)
92,769 ✔
1213
    {
1214
        _DEBUG_MSG(1, "%s disease-blocked %u heal\n", status_description(status).c_str(), status->m_diseased);
22,343 ✔
1215
        remaining_heal = heal - status->m_diseased;
22,343 ✔
1216
        status->m_diseased = 0;
22,343 ✔
1217
    }
1218
    else
1219
    {
1220
        _DEBUG_MSG(1, "%s disease-blocked %u heal\n", status_description(status).c_str(), heal);
70,426 ✔
1221
        status->m_diseased -= heal;
70,426 ✔
1222
        remaining_heal = 0;
70,426 ✔
1223
    }
1224
    return remaining_heal;
38,229,751 ✔
1225
}
1226

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

1261
void remove_hp(Field* fd, CardStatus* status, unsigned dmg)
38,275,881 ✔
1262
{
1263
    if (__builtin_expect(!dmg, false)) { return; }
38,275,881 ✔
1264
    _DEBUG_ASSERT(is_alive(status));
27,817,866 ✔
1265
    _DEBUG_MSG(2, "%s takes %u damage\n", status_description(status).c_str(), dmg);
27,817,866 ✔
1266
    status->m_hp = safe_minus(status->m_hp, dmg);
27,817,866 ✔
1267
    if (fd->current_phase < Field::end_phase && status->has_skill(Skill::barrier))
27,817,866 ✔
1268
    {
1269
        ++fd->damaged_units_to_times[status];
279,210 ✔
1270
        _DEBUG_MSG(2, "%s damaged %u times\n",
279,210 ✔
1271
                status_description(status).c_str(), fd->damaged_units_to_times[status]);
1272
    }
1273
    if (status->m_hp == 0)
27,817,866 ✔
1274
    {
1275
#ifndef NQUEST
1276
        if (status->m_player == 1)
1277
        {
1278
            if (status->m_card->m_type == CardType::assault)
1279
            {
1280
                fd->inc_counter(QuestType::faction_assault_card_kill, status->m_card->m_faction);
1281
            }
1282
            fd->inc_counter(QuestType::type_card_kill, status->m_card->m_type);
1283
        }
1284
#endif
1285
        _DEBUG_MSG(1, "%s dies\n", status_description(status).c_str());
10,494,777 ✔
1286
        _DEBUG_ASSERT(status->m_card->m_type != CardType::commander);
10,494,777 ✔
1287
        fd->killed_units.push_back(status);
10,494,777 ✔
1288
        ++fd->players[status->m_player]->total_cards_destroyed;
10,494,777 ✔
1289
        if(!status->m_summoned)++fd->players[status->m_player]->total_nonsummon_cards_destroyed;
10,494,777 ✔
1290
        if (__builtin_expect((status->m_player == 0) && (fd->players[0]->deck->vip_cards.count(status->m_card->m_id)), false))
13,631,221 ✔
1291
        {
1292
            fd->players[0]->commander.m_hp = 0;
×
1293
            fd->end = true;
×
1294
        }
1295
    }
1296
}
1297

1298
inline bool is_it_dead(CardStatus& c)
163,685,606 ✔
1299
{
1300
    if (c.m_hp == 0) // yes it is
163,685,606 ✔
1301
    {
1302
        _DEBUG_MSG(1, "Dead and removed: %s\n", status_description(&c).c_str());
10,494,777 ✔
1303
        return true;
10,494,777 ✔
1304
    }
1305
    return false; // nope still kickin'
1306
}
1307

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

1313
inline void remove_dead(Storage<CardStatus>& storage)
84,369,844 ✔
1314
{
1315
    storage.remove(is_it_dead);
84,369,844 ✔
1316
}
1317

1318
void cooldown_skills(CardStatus * status)
54,323,710 ✔
1319
{
1320
    for (const auto & ss : status->m_card->m_skills)
210,052,133 ✔
1321
    {
1322
        if (status->m_skill_cd[ss.id] > 0)
155,728,423 ✔
1323
        {
1324
            _DEBUG_MSG(2, "%s reduces timer (%u) of skill %s\n",
3,720,263 ✔
1325
                    status_description(status).c_str(), status->m_skill_cd[ss.id], skill_names[ss.id].c_str());
3,720,263 ✔
1326
            -- status->m_skill_cd[ss.id];
3,720,263 ✔
1327
        }
1328
    }
1329
}
54,323,710 ✔
1330
/**
1331
 * Handle:
1332
 * Absorb, (Activation)Summon, Bravery, (Initial)Valor, Inhibit, Sabotage, Disease, Enhance, (Cooldown/Every X) Reductions
1333
 * 
1334
 * Does not handle these skills for newly summoned units ( i.e. valor, barrier)
1335
 **/
1336
void turn_start_phase_update(Field*fd, CardStatus * status)
65,365,378 ✔
1337
{
1338
            //apply Absorb + Triggered\{Valor} Enhances
1339
            check_and_perform_early_enhance(fd,status);
130,730,756 ✔
1340
            if(fd->fixes[Fix::enhance_early])check_and_perform_later_enhance(fd,status);
65,365,378 ✔
1341
            //refresh absorb
1342
            if(status->has_skill(Skill::absorb))
65,365,378 ✔
1343
            {
1344
                status->m_absorption = status->skill_base_value(Skill::absorb);
4,479,764 ✔
1345
            }
1346
            if(__builtin_expect(fd->fixes[Fix::barrier_each_turn],true) && status->has_skill(Skill::barrier)){
65,365,378 ✔
1347
                _DEBUG_MSG(1,"%s gets barrier protection %u per turn\n",status_description(status).c_str(),status->skill(Skill::barrier));
1,253,771 ✔
1348
                status->m_protected += status->skill(Skill::barrier);
1,253,771 ✔
1349
            }
1350
            check_and_perform_bravery(fd,status);
65,365,378 ✔
1351
            if (status->m_delay > 0)
65,365,378 ✔
1352
            {
1353
                _DEBUG_MSG(1, "%s reduces its timer\n", status_description(status).c_str());
32,134,129 ✔
1354
                --status->m_delay;
32,134,129 ✔
1355
                if (status->m_delay == 0)
32,134,129 ✔
1356
                {
1357
                    check_and_perform_valor(fd, status);
13,979,474 ✔
1358
                    if (status->m_card->m_skill_trigger[Skill::summon] == Skill::Trigger::activate)
13,979,474 ✔
1359
                    {
1360
                        check_and_perform_summon(fd, status);
11,573,588 ✔
1361
                    }
1362
                }
1363
            }
1364
            else
1365
            {
1366
                cooldown_skills(status);
33,231,249 ✔
1367
            }
1368
}
65,365,378 ✔
1369

1370
void turn_start_phase(Field* fd)
21,092,461 ✔
1371
{
1372
    // Active player's commander card:
1373
    cooldown_skills(&fd->tap->commander);
21,092,461 ✔
1374
    //grab assaults before new ones get summoned
1375
    auto& assaults(fd->tap->assaults);
21,092,461 ✔
1376
    unsigned end(assaults.size());
21,092,461 ✔
1377

1378
    //Perform early enhance for commander
1379
    check_and_perform_early_enhance(fd,&fd->tap->commander);
42,184,922 ✔
1380
    if(fd->fixes[Fix::enhance_early])check_and_perform_later_enhance(fd,&fd->tap->commander);
21,092,461 ✔
1381

1382
    // Active player's structure cards:
1383
    // update index
1384
    // reduce delay; reduce skill cooldown
1385
    {
21,092,461 ✔
1386
        auto& structures(fd->tap->structures);
21,092,461 ✔
1387
        for(unsigned index(0); index < structures.size(); ++index)
45,379,756 ✔
1388
        {
1389
            CardStatus * status = &structures[index];
24,287,295 ✔
1390
            status->m_index = index;
24,287,295 ✔
1391
            turn_start_phase_update(fd,status);
24,287,295 ✔
1392
        }
1393
    }
1394
    // Active player's assault cards:
1395
    // update index
1396
    // reduce delay; reduce skill cooldown
1397
    {
1398
        for(unsigned index(0); index < end; ++index)
62,170,544 ✔
1399
        {
1400
            CardStatus * status = &assaults[index];
41,078,083 ✔
1401
            status->m_index = index;
41,078,083 ✔
1402
            turn_start_phase_update(fd,status);
41,078,083 ✔
1403
        }
1404
    }
1405
    // Defending player's structure cards:
1406
    // update index
1407
    {
21,092,461 ✔
1408
        auto& structures(fd->tip->structures);
21,092,461 ✔
1409
        for(unsigned index(0), end(structures.size()); index < end; ++index)
46,095,216 ✔
1410
        {
1411
            CardStatus& status(structures[index]);
25,002,755 ✔
1412
            status.m_index = index;
25,002,755 ✔
1413
        }
1414
    }
1415
    // Defending player's assault cards:
1416
    // update index
1417
    {
21,092,461 ✔
1418
        auto& assaults(fd->tip->assaults);
21,092,461 ✔
1419
        for(unsigned index(0), end(assaults.size()); index < end; ++index)
71,687,976 ✔
1420
        {
1421
            CardStatus& status(assaults[index]);
50,595,515 ✔
1422
            status.m_index = index;
50,595,515 ✔
1423
        }
1424
    }
1425
}
21,092,461 ✔
1426

1427
void turn_end_phase(Field* fd)
21,092,461 ✔
1428
{
1429
    // Inactive player's assault cards:
1430
    {
21,092,461 ✔
1431
        auto& assaults(fd->tip->assaults);
21,092,461 ✔
1432
        for(unsigned index(0), end(assaults.size()); index < end; ++ index)
71,770,111 ✔
1433
        {
1434
            CardStatus& status(assaults[index]);
50,677,650 ✔
1435
            if (status.m_hp <= 0)
50,677,650 ✔
1436
            {
1437
                continue;
8,677,718 ✔
1438
            }
1439
            status.m_enfeebled = 0;
41,999,932 ✔
1440
            status.m_protected = 0;
41,999,932 ✔
1441
            if (status.m_skills_modified)
41,999,932 ✔
1442
            {
1443
                std::memset(status.m_primary_skill_offset, 0, sizeof status.m_primary_skill_offset);
4,697,718 ✔
1444
                std::memset(status.m_evolved_skill_offset, 0, sizeof status.m_evolved_skill_offset);
4,697,718 ✔
1445
                std::memset(status.m_enhanced_value, 0, sizeof status.m_enhanced_value);
4,697,718 ✔
1446
                status.m_skills_modified = false;
4,697,718 ✔
1447
            }
1448
            status.m_evaded = 0;  // so far only useful in Inactive turn
41,999,932 ✔
1449
            status.m_paybacked = 0;  // ditto
41,999,932 ✔
1450
            status.m_entrapped = 0;
41,999,932 ✔
1451
        }
1452
    }
1453
    // Inactive player's structure cards:
1454
    {
21,092,461 ✔
1455
        auto& structures(fd->tip->structures);
21,092,461 ✔
1456
        for(unsigned index(0), end(structures.size()); index < end; ++ index)
46,098,763 ✔
1457
        {
1458
            CardStatus& status(structures[index]);
25,006,302 ✔
1459
            if (status.m_hp <= 0)
25,006,302 ✔
1460
            {
1461
                continue;
960,946 ✔
1462
            }
1463
            // reset the structure's (barrier) protect
1464
            status.m_protected = 0;
24,045,356 ✔
1465
            status.m_evaded = 0;  // so far only useful in Inactive turn
24,045,356 ✔
1466
        }
1467
    }
1468

1469
    // Active player's assault cards:
1470
    {
21,092,461 ✔
1471
        auto& assaults(fd->tap->assaults);
21,092,461 ✔
1472
        for(unsigned index(0), end(assaults.size()); index < end; ++ index)
81,546,526 ✔
1473
        {
1474
            CardStatus& status(assaults[index]);
60,454,065 ✔
1475
            if (status.m_hp <= 0)
60,454,065 ✔
1476
            {
1477
                continue;
834,204 ✔
1478
            }
1479
            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
59,619,861 ✔
1480

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

1526
    prepend_on_death(fd);  // poison
21,092,461 ✔
1527
    resolve_skill(fd);
21,092,461 ✔
1528
    remove_dead(fd->tap->assaults);
21,092,461 ✔
1529
    remove_dead(fd->tap->structures);
21,092,461 ✔
1530
    remove_dead(fd->tip->assaults);
21,092,461 ✔
1531
    remove_dead(fd->tip->structures);
21,092,461 ✔
1532
}
21,092,461 ✔
1533

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

1550
    return safe_minus(remove_absorption(fd,att,def->skill(Skill::counter) + att->m_enfeebled), att->protected_value());
2,104,947 ✔
1551
}
1552

1553
inline CardStatus* select_first_enemy_wall(Field* fd)
9,082,495 ✔
1554
{
1555
    for(unsigned i(0); i < fd->tip->structures.size(); ++i)
16,704,904 ✔
1556
    {
1557
        CardStatus* c(&fd->tip->structures[i]);
8,413,732 ✔
1558
        if (c->has_skill(Skill::wall) && is_alive(c)) { return c; }
8,413,732 ✔
1559
    }
1560
    return nullptr;
1561
}
1562

1563
inline CardStatus* select_first_enemy_assault(Field* fd)
1564
{
1565
    for(unsigned i(0); i < fd->tip->assaults.size(); ++i)
47,957 ✔
1566
    {
1567
        CardStatus* c(&fd->tip->assaults[i]);
37,945 ✔
1568
        if (is_alive(c)) { return c; }
37,945 ✔
1569
    }
1570
    return nullptr;
1571
}
1572

1573

1574
inline bool alive_assault(Storage<CardStatus>& assaults, unsigned index)
25,387,812 ✔
1575
{
1576
    return(assaults.size() > index && is_alive(&assaults[index]));
18,046,050 ✔
1577
}
1578

1579
void remove_commander_hp(Field* fd, CardStatus& status, unsigned dmg)
8,288,983 ✔
1580
{
1581
    _DEBUG_ASSERT(is_alive(&status));
8,288,983 ✔
1582
    _DEBUG_ASSERT(status.m_card->m_type == CardType::commander);
8,288,983 ✔
1583
    _DEBUG_MSG(2, "%s takes %u damage\n", status_description(&status).c_str(), dmg);
8,288,983 ✔
1584
    status.m_hp = safe_minus(status.m_hp, dmg);
8,288,983 ✔
1585
    if (status.m_hp == 0)
8,288,983 ✔
1586
    {
1587
        _DEBUG_MSG(1, "%s dies\n", status_description(&status).c_str());
1,520,689 ✔
1588
        fd->end = true;
1,520,689 ✔
1589
    }
1590
}
8,288,983 ✔
1591

1592
//------------------------------------------------------------------------------
1593
// implementation of one attack by an assault card, against either an enemy
1594
// assault card, the first enemy wall, or the enemy commander.
1595
struct PerformAttack
1596
{
1597
    Field* fd;
1598
    CardStatus* att_status;
1599
    CardStatus* def_status;
1600
    unsigned att_dmg;
1601

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

1640

1641
            if(__builtin_expect(def_status->has_skill(Skill::flying),false) && fd->flip()) {
25,387,812 ✔
1642
                _DEBUG_MSG(1, "%s flies away from %s\n",
102,562 ✔
1643
                        status_description(def_status).c_str(),
1644
                        status_description(att_status).c_str());
1645
                return 0;
51,281 ✔
1646
            }
1647

1648
            if ( __builtin_expect(fd->fixes[Fix::subdue_before_attack],true)) {
25,336,531 ✔
1649
                perform_subdue(fd, att_status, def_status);
25,336,531 ✔
1650
                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."
25,336,531 ✔
1651
                { 
1652
                    return 0; 
1653
                }
1654
            }
1655
            
1656
            // APN Bug fix for subdue not reducing attack damage
1657
            // https://github.com/APN-Pucky/tyrant_optimize/issues/79
1658
            unsigned pre_modifier_dmg = att_status->attack_power();
25,331,038 ✔
1659

1660
            modify_attack_damage<def_cardtype>(pre_modifier_dmg);
25,331,038 ✔
1661

1662
            if(att_dmg) {
25,331,038 ✔
1663
                // Deal the attack damage
1664
                attack_damage<def_cardtype>();
21,156,472 ✔
1665

1666
                // Game Over -> return
1667
                if (__builtin_expect(fd->end, false)) { return att_dmg; }
21,156,472 ✔
1668
                damage_dependant_pre_oa<def_cardtype>();
12,076,166 ✔
1669
            }
1670
            // on attacked does also trigger if the attack damage is blocked to zero
1671
            on_attacked<def_cardtype>();
23,810,349 ✔
1672

1673
            // only if alive attacker
1674
            if (is_alive(att_status) && (__builtin_expect(fd->fixes[Fix::counter_without_damage],true) || att_dmg) )
23,810,349 ✔
1675
            {
1676
                perform_counter<def_cardtype>(fd, att_status, def_status);    
23,798,519 ✔
1677
            }
1678
            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"
23,810,349 ✔
1679
            {
1680
                perform_corrosive(fd, att_status, def_status);
23,073,534 ✔
1681
            }
1682
            if (is_alive(att_status) && att_dmg)
23,810,349 ✔
1683
            {
1684
                // Bug fix? 2023-04-03 leech after counter but before drain/berserk
1685
                // Skill: Leech
1686
                do_leech<def_cardtype>();
11,911,030 ✔
1687
            }
1688

1689
            // Bug fix? 2023-04-03 drain now part of the PerformAttack.op() instead of attack_phase, like hunt.
1690
            // perform swipe/drain
1691
            perform_swipe_drain<def_cardtype>(fd, att_status, def_status, att_dmg);
23,810,349 ✔
1692

1693
            if (is_alive(att_status) && att_dmg) {
23,810,349 ✔
1694
                perform_berserk(fd, att_status, def_status);
18,964,700 ✔
1695
            }
1696
            if (is_alive(att_status) )  {
23,810,349 ✔
1697
                perform_poison(fd, att_status, def_status);
23,073,533 ✔
1698
            }
1699

1700
            if (is_alive(att_status) && att_dmg) {
23,810,349 ✔
1701
                perform_mark(fd, att_status, def_status);
18,964,700 ✔
1702

1703
                perform_bge_heroism<def_cardtype>(fd, att_status, def_status);
18,964,700 ✔
1704
                perform_bge_devour<def_cardtype>(fd, att_status, def_status);
18,964,700 ✔
1705

1706
                if ( __builtin_expect(!fd->fixes[Fix::subdue_before_attack],false)) {
18,964,700 ✔
1707
                    perform_subdue(fd, att_status, def_status);
×
1708
                }
1709
            }
1710
            // perform hunt
1711
            perform_hunt(fd, att_status, def_status);
23,810,349 ✔
1712

1713
            return att_dmg;
23,810,349 ✔
1714
        }
1715

1716
        /**
1717
         * @brief Modify attack damage
1718
         * This setts att_dmg in PerformAttack.
1719
         * 
1720
         * @tparam CardType::CardType 
1721
         * @param pre_modifier_dmg base damage
1722
         */
1723
    template<enum CardType::CardType>
1724
        void modify_attack_damage(unsigned pre_modifier_dmg)
25,331,038 ✔
1725
        {
1726
            _DEBUG_ASSERT(att_status->m_card->m_type == CardType::assault);
25,331,038 ✔
1727
            att_dmg = pre_modifier_dmg;
25,331,038 ✔
1728
            if (att_dmg == 0)
25,331,038 ✔
1729
            { return; }
×
1730
#ifndef NDEBUG
1731
            std::string desc;
25,331,038 ✔
1732
#endif
1733
            auto& att_assaults = fd->tap->assaults; // (active) attacker assaults
25,331,038 ✔
1734
            auto& def_assaults = fd->tip->assaults; // (inactive) defender assaults
25,331,038 ✔
1735
            unsigned legion_value = 0;
25,331,038 ✔
1736
            unsigned coalition_value = 0;
25,331,038 ✔
1737

1738
            // Enhance damage (if additional damage isn't prevented)
1739
            if (! att_status->m_sundered)
25,331,038 ✔
1740
            {
1741
                // Skill: Mark
1742
                unsigned marked_value = def_status->m_marked;
23,874,658 ✔
1743
                if(marked_value)
23,874,658 ✔
1744
                {
1745
#ifndef NDEBUG
1746
                    if (debug_print > 0) { desc += "+" + tuo::to_string(marked_value) + "(mark)"; }
47,227 ✔
1747
#endif
1748
                    att_dmg += marked_value;
11,974 ✔
1749
                }
1750
                // Skill: Legion
1751
                unsigned legion_base = att_status->skill(Skill::legion);
23,874,658 ✔
1752
                if (__builtin_expect(legion_base, false))
23,874,658 ✔
1753
                {
1754
                    unsigned itr_idx, till_idx;
1755
                    bool bge_megamorphosis = fd->bg_effects[fd->tapi][PassiveBGE::megamorphosis] && !fd->fixes[Fix::legion_under_mega];
2,924,743 ✔
1756
                    //scan all assaults for Global Legion
1757
                    itr_idx = 0;
2,924,743 ✔
1758
                    till_idx = att_assaults.size() - 1;
2,924,743 ✔
1759
                    for (; itr_idx <= till_idx; ++ itr_idx)
19,203,917 ✔
1760
                    {
1761
                        if(itr_idx == att_status->m_index)continue; //legion doesn't count itself, unlike coalition
16,279,174 ✔
1762
                        legion_value += is_alive(&att_assaults[itr_idx])
26,686,152 ✔
1763
                          && (bge_megamorphosis || (att_assaults[itr_idx].m_card->m_faction == att_status->m_card->m_faction));
13,354,431 ✔
1764
                    }
1765
                    if (legion_value)
2,924,743 ✔
1766
                    {
1767
                        legion_value *= legion_base;
2,739,890 ✔
1768
#ifndef NDEBUG
1769
                        if (debug_print > 0) { desc += "+" + tuo::to_string(legion_value) + "(legion)"; }
3,003,452 ✔
1770
#endif
1771
                        att_dmg += legion_value;
2,739,890 ✔
1772
                    }
1773
                }
1774

1775
                // Skill: Coalition
1776
                unsigned coalition_base = att_status->skill(Skill::coalition);
23,874,658 ✔
1777
                if (__builtin_expect(coalition_base, false))
23,874,658 ✔
1778
                {
1779
                    uint8_t factions_bitmap = 0;
1,834,745 ✔
1780
                    for (CardStatus * status : att_assaults.m_indirect)
9,635,360 ✔
1781
                    {
1782
                        if (! is_alive(status)) { continue; }
7,800,615 ✔
1783
                        factions_bitmap |= (1 << (status->m_card->m_faction));
7,794,877 ✔
1784
                    }
1785
                    _DEBUG_ASSERT(factions_bitmap);
1,834,745 ✔
1786
                    unsigned uniq_factions = byte_bits_count(factions_bitmap);
1,834,745 ✔
1787
                    coalition_value = coalition_base * uniq_factions;
1,834,745 ✔
1788
#ifndef NDEBUG
1789
                    if (debug_print > 0) { desc += "+" + tuo::to_string(coalition_value) + "(coalition/x" + tuo::to_string(uniq_factions) + ")"; }
2,367,797 ✔
1790
#endif
1791
                    att_dmg += coalition_value;
1,834,745 ✔
1792
                }
1793

1794
                // Skill: Rupture
1795
                unsigned rupture_value = att_status->skill(Skill::rupture);
23,874,658 ✔
1796
                if (rupture_value > 0)
23,874,658 ✔
1797
                {
1798
#ifndef NDEBUG
1799
                    if (debug_print > 0) { desc += "+" + tuo::to_string(rupture_value) + "(rupture)"; }
×
1800
#endif
1801
                    att_dmg += rupture_value;
×
1802
                }
1803

1804
                // Skill: Venom
1805
                unsigned venom_value = att_status->skill(Skill::venom);
23,874,658 ✔
1806
                if (venom_value > 0 && def_status->m_poisoned > 0)
23,874,658 ✔
1807
                {
1808
#ifndef NDEBUG
1809
                    if (debug_print > 0) { desc += "+" + tuo::to_string(venom_value) + "(venom)"; }
145,957 ✔
1810
#endif
1811
                    att_dmg += venom_value;
94,156 ✔
1812
                }
1813

1814
                // Passive BGE: Bloodlust
1815
                if (fd->bloodlust_value > 0)
23,874,658 ✔
1816
                {
1817
#ifndef NDEBUG
1818
                    if (debug_print > 0) { desc += "+" + tuo::to_string(fd->bloodlust_value) + "(bloodlust)"; }
205,544 ✔
1819
#endif
1820
                    att_dmg += fd->bloodlust_value;
51,386 ✔
1821
                }
1822

1823
                // State: Enfeebled
1824
                if (def_status->m_enfeebled > 0)
23,874,658 ✔
1825
                {
1826
#ifndef NDEBUG
1827
                    if (debug_print > 0) { desc += "+" + tuo::to_string(def_status->m_enfeebled) + "(enfeebled)"; }
3,154,983 ✔
1828
#endif
1829
                    att_dmg += def_status->m_enfeebled;
2,240,511 ✔
1830
                }
1831
            }
1832
            // prevent damage
1833
#ifndef NDEBUG
1834
            std::string reduced_desc;
25,331,038 ✔
1835
#endif
1836
            unsigned reduced_dmg(0); // damage reduced by armor, protect and in turn again canceled by pierce, etc.
25,331,038 ✔
1837
            unsigned armor_value = 0;
25,331,038 ✔
1838
            // Armor
1839
            if (def_status->m_card->m_type == CardType::assault) {
25,331,038 ✔
1840
                // Passive BGE: Fortification (adj step -> 1 (1 left, host, 1 right)
1841
                unsigned adj_size = (unsigned)(fd->bg_effects[fd->tapi][PassiveBGE::fortification]);
16,250,732 ✔
1842
                unsigned host_idx = def_status->m_index;
16,250,732 ✔
1843
                unsigned from_idx = safe_minus(host_idx, adj_size);
16,250,732 ✔
1844
                unsigned till_idx = std::min(host_idx + adj_size, safe_minus(def_assaults.size(), 1));
30,288,869 ✔
1845
                for (; from_idx <= till_idx; ++ from_idx)
32,529,625 ✔
1846
                {
1847
                    CardStatus* adj_status = &def_assaults[from_idx];
16,278,893 ✔
1848
                    if (!is_alive(adj_status)) { continue; }
16,278,893 ✔
1849
                    armor_value = std::max(armor_value, adj_status->skill(Skill::armor));
20,862,463 ✔
1850
                }
1851
            }
1852
            if (armor_value > 0)
25,331,038 ✔
1853
            {
1854
#ifndef NDEBUG
1855
                if(debug_print > 0) { reduced_desc += tuo::to_string(armor_value) + "(armor)"; }
4,916,207 ✔
1856
#endif
1857
                reduced_dmg += armor_value;
1858
            }
1859
            if (def_status->protected_value() > 0)
25,331,038 ✔
1860
            {
1861
#ifndef NDEBUG
1862
                if(debug_print > 0) { reduced_desc += (reduced_desc.empty() ? "" : "+") + tuo::to_string(def_status->protected_value()) + "(protected)"; }
12,770,089 ✔
1863
#endif
1864
                reduced_dmg += def_status->protected_value();
10,238,998 ✔
1865
            }
1866
            unsigned pierce_value = att_status->skill(Skill::pierce);
25,331,038 ✔
1867
            if (reduced_dmg > 0 && pierce_value > 0)
25,331,038 ✔
1868
            {
1869
#ifndef NDEBUG
1870
                if (debug_print > 0) { reduced_desc += "-" + tuo::to_string(pierce_value) + "(pierce)"; }
712,935 ✔
1871
#endif
1872
                reduced_dmg = safe_minus(reduced_dmg, pierce_value);
25,331,038 ✔
1873
            }
1874
            unsigned rupture_value = att_status->skill(Skill::rupture);
25,331,038 ✔
1875
            if (reduced_dmg > 0 && rupture_value > 0)
25,331,038 ✔
1876
            {
1877
#ifndef NDEBUG
1878
                if (debug_print > 0) { reduced_desc += "-" + tuo::to_string(rupture_value) + "(rupture)"; }
×
1879
#endif
1880
                reduced_dmg = safe_minus(reduced_dmg, rupture_value);
25,331,038 ✔
1881
            }
1882
            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."
25,331,038 ✔
1883
            {
1884
                unsigned corrosive_value = def_status->m_corroded_rate;
25,331,038 ✔
1885
                if (reduced_dmg > 0 && corrosive_value > 0)
25,331,038 ✔
1886
                {
1887
#ifndef NDEBUG
1888
                    if (debug_print > 0) { reduced_desc += "-" + tuo::to_string(corrosive_value) + "(corrosive)"; }
35,167 ✔
1889
#endif
1890
                    reduced_dmg = safe_minus(reduced_dmg, corrosive_value);
25,331,038 ✔
1891
                }
1892
            }
1893
            att_dmg = safe_minus(att_dmg, reduced_dmg);
25,331,038 ✔
1894
#ifndef NDEBUG
1895
            if (debug_print > 0)
25,331,038 ✔
1896
            {
1897
                if(!reduced_desc.empty()) { desc += "-[" + reduced_desc + "]"; }
4,713,447 ✔
1898
                if(!desc.empty()) { desc += "=" + tuo::to_string(att_dmg); }
5,296,043 ✔
1899
                else { assert(att_dmg == pre_modifier_dmg); }
1,662,635 ✔
1900
                _DEBUG_MSG(1, "%s attacks %s for %u%s damage\n",
5,747,542 ✔
1901
                        status_description(att_status).c_str(),
1902
                        status_description(def_status).c_str(), pre_modifier_dmg, desc.c_str());
1903
            }
1904
#endif
1905
            // Passive BGE: Brigade
1906
            if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::brigade] && legion_value, false)
25,331,038 ✔
1907
                    && can_be_healed(att_status))
×
1908
            {
1909
                _DEBUG_MSG(1, "Brigade: %s heals itself for %u\n",
×
1910
                        status_description(att_status).c_str(), legion_value);
1911
                att_status->add_hp(legion_value);
×
1912
            }
1913

1914
            // Passive BGE: Unity
1915
            if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::unity] && coalition_value, false)
25,331,038 ✔
1916
                    && can_be_healed(att_status))
25,334,564 ✔
1917
            {
1918
                // Unity no longer uses the legacy half-heal scaling (issue #109).
1919
                _DEBUG_MSG(1, "Unity: %s heals itself for %u\n", status_description(att_status).c_str(), coalition_value);
898 ✔
1920
                att_status->add_hp(coalition_value);
449 ✔
1921
            }
1922
        }
25,331,038 ✔
1923

1924
    template<enum CardType::CardType>
1925
        void attack_damage()
12,867,489 ✔
1926
        {
1927

1928
            remove_hp(fd, def_status, att_dmg);
12,867,489 ✔
1929
            prepend_on_death(fd);
12,867,489 ✔
1930
            resolve_skill(fd);
12,867,489 ✔
1931
        }
12,867,489 ✔
1932

1933
    template<enum CardType::CardType>
1934
        void on_attacked() {
23,810,349 ✔
1935
            //APN
1936
            // resolve On-Attacked skills
1937
            for (const auto& ss: def_status->m_card->m_skills_on_attacked)
24,008,068 ✔
1938
            {
1939
                _DEBUG_MSG(1, "On Attacked %s: Preparing (tail) skill %s\n",
395,438 ✔
1940
                        status_description(def_status).c_str(), skill_description(fd->cards, ss).c_str());
1941
                fd->skill_queue.emplace_back(def_status, ss);
197,719 ✔
1942
                resolve_skill(fd);
197,719 ✔
1943
            }
1944
        }
23,810,349 ✔
1945

1946
    template<enum CardType::CardType>
1947
        void damage_dependant_pre_oa() {}
1948

1949
    template<enum CardType::CardType>
1950
        void do_leech() {}
1951

1952
    template<enum CardType::CardType>
1953
        void perform_swipe_drain(Field* fd, CardStatus* att_status, CardStatus* def_status,unsigned att_dmg) {}
1954
    };
1955

1956

1957
    template<>
1958
void PerformAttack::attack_damage<CardType::commander>()
8,288,983 ✔
1959
{
1960
    remove_commander_hp(fd, *def_status, att_dmg);
8,288,983 ✔
1961
}
×
1962

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

1995
              _DEBUG_MSG(1, "%s venoms %s by %u\n",
477,528 ✔
1996
                  status_description(att_status).c_str(),
1997
                  status_description(def_status).c_str(), venom_value);
477,528 ✔
1998
              // new venom keeps adding stacks
1999
              def_status->m_poisoned += venom_value;
477,528 ✔
2000
        }
2001
    }
2002
}
12,076,166 ✔
2003

2004

2005
    template<>
2006
void PerformAttack::do_leech<CardType::assault>()
11,911,030 ✔
2007
{
2008
    unsigned leech_value = std::min(att_dmg, att_status->skill(Skill::leech));
11,911,030 ✔
2009
    if(leech_value > 0 && skill_check<Skill::leech>(fd, att_status, nullptr))
11,911,030 ✔
2010
    {
2011
#ifndef NQUEST
2012
        if (att_status->m_player == 0)
2013
        {
2014
            fd->inc_counter(QuestType::skill_use, Skill::leech);
2015
        }
2016
#endif
2017
        _DEBUG_MSG(1, "%s leeches %u health\n", status_description(att_status).c_str(), leech_value);
47,256 ✔
2018
        if (__builtin_expect(fd->fixes[Fix::leech_increase_max_hp],true)) {
47,256 ✔
2019
            att_status->ext_hp(leech_value);
47,256 ✔
2020
        }
2021
        else {
2022
            att_status->add_hp(leech_value);
×
2023
        }
2024
    }
2025
}
11,911,030 ✔
2026

2027
// General attack phase by the currently evaluated assault, taking into accounts exotic stuff such as flurry, etc.
2028
unsigned attack_commander(Field* fd, CardStatus* att_status)
9,082,495 ✔
2029
{
2030
    CardStatus* def_status{select_first_enemy_wall(fd)}; // defending wall
9,082,495 ✔
2031
    if (def_status != nullptr)
9,082,495 ✔
2032
    {
2033
        return PerformAttack{fd, att_status, def_status}.op<CardType::structure>();
791,323 ✔
2034
    }
2035
    else
2036
    {
2037
        return PerformAttack{fd, att_status, &fd->tip->commander}.op<CardType::commander>();
8,291,172 ✔
2038
    }
2039
}
2040
// Return true if actually attacks
2041
bool attack_phase(Field* fd)
28,131,214 ✔
2042
{
2043
    CardStatus* att_status(&fd->tap->assaults[fd->current_ci]); // attacking card
28,131,214 ✔
2044
    Storage<CardStatus>& def_assaults(fd->tip->assaults);
28,131,214 ✔
2045

2046
    if (!att_status->attack_power())
28,131,214 ✔
2047
    {
2048
        _DEBUG_MSG(1, "%s cannot take attack (zeroed)\n", status_description(att_status).c_str());
2,743,402 ✔
2049
        return false;
2,743,402 ✔
2050
    }
2051

2052
    unsigned att_dmg = 0;
25,387,812 ✔
2053
    if (alive_assault(def_assaults, fd->current_ci))
25,387,812 ✔
2054
    {
2055
        CardStatus* def_status = &def_assaults[fd->current_ci];
16,305,317 ✔
2056
        att_dmg = PerformAttack{fd, att_status, def_status}.op<CardType::assault>();
16,305,317 ✔
2057
        
2058
    }
2059
    else
2060
    {
2061
        // might be blocked by walls
2062
        att_dmg = attack_commander(fd, att_status);
9,082,495 ✔
2063
    }
2064

2065
    // Passive BGE: Bloodlust
2066
    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::bloodlust], false)
25,387,812 ✔
2067
            && !fd->assault_bloodlusted && (att_dmg > 0))
25,387,812 ✔
2068
    {
2069
        fd->bloodlust_value += fd->bg_effects[fd->tapi][PassiveBGE::bloodlust];
113,021 ✔
2070
        fd->assault_bloodlusted = true;
113,021 ✔
2071
    }
2072

2073
    return true;
2074
}
2075

2076
//---------------------- $65 active skills implementation ----------------------
2077
template<
2078
bool C
2079
, typename T1
2080
, typename T2
2081
>
2082
struct if_
2083
{
2084
    typedef T1 type;
2085
};
2086

2087
template<
2088
typename T1
2089
, typename T2
2090
>
2091
struct if_<false,T1,T2>
2092
{
2093
    typedef T2 type;
2094
};
2095

2096
    template<unsigned skill_id>
2097
inline bool skill_predicate(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
274 ✔
2098
{ return skill_check<static_cast<Skill::Skill>(skill_id)>(fd, dst, src); }
393,674,684 ✔
2099

2100
    template<>
2101
inline bool skill_predicate<Skill::enhance>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
34,604,414 ✔
2102
{
2103
    if (!is_alive(dst)) return false;
34,604,414 ✔
2104
    if (!dst->has_skill(s.s)) return false;
69,208,828 ✔
2105
    if (is_active(dst)) return true;
7,817,036 ✔
2106
    if (is_defensive_skill(s.s)) return true;
4,059,122 ✔
2107
    if (is_instant_debuff_skill(s.s)) return true; // Enhance Sabotage, Inhibit, Disease also without dst being active
734,660 ✔
2108
    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)
734,560 ✔
2109

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

2118
    template<>
2119
inline bool skill_predicate<Skill::evolve>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
3,762,661 ✔
2120
{
2121
    if (!is_alive(dst)) return false;
3,762,661 ✔
2122
    if (!dst->has_skill(s.s)) return false;
7,519,620 ✔
2123
    if (dst->has_skill(s.s2)) return false;
2,280,284 ✔
2124
    if (is_active(dst)) return true;
1,140,142 ✔
2125
    if (is_defensive_skill(s.s2)) return true;
661,492 ✔
2126

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

2135
    template<>
2136
inline bool skill_predicate<Skill::mimic>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
510,943 ✔
2137
{
2138
    // skip dead units
2139
    if (!is_alive(dst)) return false;
510,943 ✔
2140

2141
    //include on activate/attacked/death
2142
    //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})
2143
    //
2144
    std::vector<std::vector<SkillSpec>> all;
465,467 ✔
2145
    all.emplace_back(dst->m_card->m_skills);
465,467 ✔
2146
    all.emplace_back(dst->m_card->m_skills_on_attacked);
465,467 ✔
2147

2148
    for(std::vector<SkillSpec> & a  : all)
750,412 ✔
2149
    {
2150
    // scan all enemy skills until first activation
2151
    for (const SkillSpec & ss: a)
1,433,464 ✔
2152
    {
2153
        // get skill
2154
        Skill::Skill skill_id = static_cast<Skill::Skill>(ss.id);
1,148,519 ✔
2155

2156
        // skip non-activation skills and Mimic (Mimic can't be mimicked)
2157
        if (!is_activation_skill(skill_id) || (skill_id == Skill::mimic))
1,481,291 ✔
2158
        { continue; }
815,747 ✔
2159

2160
        // skip mend for non-assault mimickers
2161
        if ((skill_id == Skill::mend || skill_id == Skill::fortify) && (src->m_card->m_type != CardType::assault))
332,772 ✔
2162
        { continue; }
2,800 ✔
2163

2164
        // enemy has at least one activation skill that can be mimicked, so enemy is eligible target for Mimic
2165
        return true;
465,467 ✔
2166
    }
2167
    }
2168

2169
    // found nothing (enemy has no skills to be mimicked, so enemy isn't eligible target for Mimic)
2170
    return false;
2171
}
465,467 ✔
2172

2173
    template<>
2174
inline bool skill_predicate<Skill::overload>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
702,763 ✔
2175
{
2176
    // basic skill check
2177
    if (!skill_check<Skill::overload>(fd, dst, src))
1,123,830 ✔
2178
    { return false; }
2179

2180
    // check skills
2181
    bool inhibited_searched = false;
352,152 ✔
2182
    for (const auto& ss: dst->m_card->m_skills)
980,037 ✔
2183
    {
2184
        // skip cooldown skills
2185
        if (dst->m_skill_cd[ss.id] > 0)
907,454 ✔
2186
        { continue; }
7,974 ✔
2187

2188
        // get evolved skill
2189
        Skill::Skill evolved_skill_id = static_cast<Skill::Skill>(ss.id + dst->m_evolved_skill_offset[ss.id]);
899,480 ✔
2190

2191
        // unit with an activation hostile skill is always valid target for OL
2192
        if (is_activation_hostile_skill(evolved_skill_id))
1,178,151 ✔
2193
        { return true; }
2194

2195
        // unit with an activation helpful skill is valid target only when there are inhibited units
2196
        // TODO check mend/fortify valid overload target?!?
2197
        if ((evolved_skill_id != Skill::mend && evolved_skill_id != Skill::fortify)
622,664 ✔
2198
                && is_activation_helpful_skill(evolved_skill_id)
627,885 ✔
2199
                && __builtin_expect(!inhibited_searched, true))
812,939 ✔
2200
        {
2201
            for (const auto & c: fd->players[dst->m_player]->assaults.m_indirect)
1,052,479 ✔
2202
            {
2203
                if (is_alive(c) && c->m_inhibited)
866,304 ✔
2204
                { return true; }
279,569 ✔
2205
            }
2206
            inhibited_searched = true;
2207
        }
2208
    }
2209
    return false;
2210
}
2211

2212
    template<>
2213
inline bool skill_predicate<Skill::rally>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
30,597,551 ✔
2214
{
2215
    return skill_check<Skill::rally>(fd, dst, src) // basic skill check
30,597,551 ✔
2216
        && (__builtin_expect((fd->tapi == dst->m_player), true) // is target on the active side?
30,597,551 ✔
2217
                ? is_active(dst) && !has_attacked(dst) // normal case
17,435,054 ✔
2218
                : is_active_next_turn(dst) // diverted case / on-death activation
30,597,551 ✔
2219
           )
30,597,551 ✔
2220
        ;
2221
}
2222

2223
    template<>
2224
inline bool skill_predicate<Skill::enrage>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
35,034,090 ✔
2225
{
2226
    return (__builtin_expect((fd->tapi == dst->m_player), true) // is target on the active side?
35,034,090 ✔
2227
            ? is_active(dst) && (dst->m_step == CardStep::none) // normal case
15,918,461 ✔
2228
            : is_active_next_turn(dst) // on-death activation
50,413,434 ✔
2229
           )
2230
        && (dst->attack_power()) // card can perform direct attack
50,413,434 ✔
2231
        ;
2232
}
2233

2234
    template<>
2235
inline bool skill_predicate<Skill::rush>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
×
2236
{
2237
    return (!src->m_rush_attempted)
×
2238
        && (dst->m_delay >= ((src->m_card->m_type == CardType::assault) && (dst->m_index < src->m_index) ? 2u : 1u));
×
2239
}
2240

2241
    template<>
2242
inline bool skill_predicate<Skill::weaken>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
40,992,561 ✔
2243
{
2244
    if(__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::ironwill], false) && dst->has_skill(Skill::armor))return false;
40,992,561 ✔
2245
    if (!dst->attack_power()) { return false; }
40,989,001 ✔
2246

2247
    // active player performs Weaken (normal case)
2248
    if (__builtin_expect((fd->tapi == src->m_player), true))
36,668,535 ✔
2249
    { return is_active_next_turn(dst); }
36,554,056 ✔
2250

2251
    // APN - On-Attacked/Death don't target the attacking card
2252

2253
    // inactive player performs Weaken (inverted case (on-death activation))
2254
    return will_activate_this_turn(dst);
114,479 ✔
2255
}
2256

2257
    template<>
2258
inline bool skill_predicate<Skill::sunder>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
18,629,318 ✔
2259
{
2260
    return skill_predicate<Skill::weaken>(fd, src, dst, s);
105 ✔
2261
}
2262

2263
    template<unsigned skill_id>
2264
inline void perform_skill(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
2265
{ assert(false); }
2266

2267
    template<>
2268
inline void perform_skill<Skill::enfeeble>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
8,191,000 ✔
2269
{
2270
    dst->m_enfeebled += s.x;
8,191,000 ✔
2271
}
×
2272

2273
    template<>
2274
inline void perform_skill<Skill::enhance>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
6,644,780 ✔
2275
{
2276
    dst->m_enhanced_value[s.s + dst->m_primary_skill_offset[s.s]] += s.x;
6,644,780 ✔
2277
    dst->m_skills_modified = true;
6,644,780 ✔
2278
}
22 ✔
2279

2280
    template<>
2281
inline void perform_skill<Skill::evolve>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
462,561 ✔
2282
{
2283
    auto primary_s1 = dst->m_primary_skill_offset[s.s] + s.s;
462,561 ✔
2284
    auto primary_s2 = dst->m_primary_skill_offset[s.s2] + s.s2;
462,561 ✔
2285
    dst->m_primary_skill_offset[s.s] = primary_s2 - s.s;
462,561 ✔
2286
    dst->m_primary_skill_offset[s.s2] = primary_s1 - s.s2;
462,561 ✔
2287
    dst->m_evolved_skill_offset[primary_s1] = s.s2 - primary_s1;
462,561 ✔
2288
    dst->m_evolved_skill_offset[primary_s2] = s.s - primary_s2;
462,561 ✔
2289
    dst->m_skills_modified = true;
462,561 ✔
UNCOV
2290
}
×
2291

2292
    template<>
2293
inline void perform_skill<Skill::heal>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
8,949,451 ✔
2294
{
2295
    dst->add_hp(s.x);
8,949,451 ✔
2296

2297
    // Passive BGE: ZealotsPreservation
2298
    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::zealotspreservation], false)
8,949,451 ✔
2299
            && (src->m_card->m_type == CardType::assault))
8,949,451 ✔
2300
    {
2301
        unsigned bge_value = (s.x + 1) / 2;
342 ✔
2302
        _DEBUG_MSG(1, "Zealot's Preservation: %s Protect %u on %s\n",
342 ✔
2303
                status_description(src).c_str(), bge_value,
2304
                status_description(dst).c_str());
342 ✔
2305
        dst->m_protected += bge_value;
342 ✔
2306
    }
2307
}
8,949,451 ✔
2308

2309
    template<>
2310
inline void perform_skill<Skill::jam>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
1,977,468 ✔
2311
{
2312
    dst->m_jammed = true;
1,977,468 ✔
2313
}
×
2314

2315
    template<>
2316
inline void perform_skill<Skill::mend>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
4,134 ✔
2317
{
2318
    dst->add_hp(s.x);
4,133 ✔
2319
}
1 ✔
2320

2321
    template<>
2322
inline void perform_skill<Skill::fortify>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
2,418,820 ✔
2323
{
2324
    dst->ext_hp(s.x);
1,761,359 ✔
2325
}
657,461 ✔
2326

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

2339
    template<>
2340
inline void perform_skill<Skill::mortar>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
3,106,080 ✔
2341
{
2342
    if (dst->m_card->m_type == CardType::structure)
3,106,080 ✔
2343
    {
2344
        perform_skill<Skill::siege>(fd, src, dst, s);
2,451,999 ✔
2345
    }
2346
    else
2347
    {
2348
        unsigned strike_dmg = remove_absorption(fd,dst,(s.x + 1) / 2 + dst->m_enfeebled);
654,081 ✔
2349
        strike_dmg = safe_minus(strike_dmg, src->m_overloaded ? 0 : dst->protected_value());
654,081 ✔
2350
        remove_hp(fd, dst, strike_dmg);
654,081 ✔
2351
    }
2352
}
3,106,080 ✔
2353

2354
    template<>
2355
inline void perform_skill<Skill::overload>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
251,159 ✔
2356
{
2357
    dst->m_overloaded = true;
251,159 ✔
2358
}
10 ✔
2359

2360
    template<>
2361
inline void perform_skill<Skill::protect>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
39,635,922 ✔
2362
{
2363
    dst->m_protected += s.x;
39,635,922 ✔
2364
}
419 ✔
2365

2366
    template<>
2367
inline void perform_skill<Skill::rally>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
13,368,201 ✔
2368
{
2369
    dst->m_temp_attack_buff += s.x;
13,368,201 ✔
2370
}
227,303 ✔
2371

2372
    template<>
2373
inline void perform_skill<Skill::enrage>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
10,764,961 ✔
2374
{
2375
    dst->m_enraged += s.x;
10,764,961 ✔
2376
    // Passive BGE: Furiosity
2377
    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::furiosity], false)
10,764,961 ✔
2378
            && can_be_healed(dst))
10,764,961 ✔
2379
    {
2380
        unsigned bge_value = s.x;
6,698 ✔
2381
        _DEBUG_MSG(1, "Furiosity: %s Heals %s for %u\n",
6,698 ✔
2382
                status_description(src).c_str(),
2383
                status_description(dst).c_str(), bge_value);
6,698 ✔
2384
        dst->add_hp(bge_value);
6,698 ✔
2385
    }
2386
}
10,764,961 ✔
2387

2388
    template<>
2389
inline void perform_skill<Skill::entrap>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
5,892,236 ✔
2390
{
2391
    dst->m_entrapped += s.x;
5,892,236 ✔
2392
}
87,155 ✔
2393

2394
    template<>
2395
inline void perform_skill<Skill::rush>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
×
2396
{
2397
    dst->m_delay -= std::min(std::max(s.x, 1u), dst->m_delay);
×
2398
    if (dst->m_delay == 0)
×
2399
    {
2400
        check_and_perform_valor(fd, dst);
×
2401
        if(dst->m_card->m_skill_trigger[Skill::summon] == Skill::Trigger::activate)check_and_perform_summon(fd, dst);
×
2402
    }
2403
}
×
2404

2405
    template<>
2406
inline void perform_skill<Skill::strike>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
18,721,631 ✔
2407
{
2408
    unsigned strike_dmg = remove_absorption(fd,dst,s.x+ dst->m_enfeebled);
18,721,631 ✔
2409
    strike_dmg = safe_minus(strike_dmg , src->m_overloaded ? 0 : dst->protected_value());
18,721,631 ✔
2410
    remove_hp(fd, dst, strike_dmg);
18,721,631 ✔
2411
}
18,721,631 ✔
2412

2413
    template<>
2414
inline void perform_skill<Skill::weaken>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
10,909,365 ✔
2415
{
2416
    dst->m_temp_attack_buff -= (unsigned)std::min(s.x, dst->attack_power());
10,909,365 ✔
2417
}
×
2418

2419
    template<>
2420
inline void perform_skill<Skill::sunder>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
3,591,179 ✔
2421
{
2422
    dst->m_sundered = true;
3,591,179 ✔
2423
    perform_skill<Skill::weaken>(fd, src, dst, s);
3,531,959 ✔
2424
}
×
2425

2426
    template<>
2427
inline void perform_skill<Skill::mimic>(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s)
206,333 ✔
2428
{
2429
    // collect all mimickable enemy skills
2430
    std::vector<const SkillSpec *> mimickable_skills;
206,333 ✔
2431
    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());
206,333 ✔
2432
    _DEBUG_MSG(2, " * Mimickable skills of %s\n", status_description(dst).c_str());
206,333 ✔
2433
    //include on activate/attacked
2434
    for(const std::vector<SkillSpec> * a : {&dst->m_card->m_skills, &dst->m_card->m_skills_on_attacked})
618,999 ✔
2435
    {
2436
    for (const SkillSpec & ss: *a)
1,030,496 ✔
2437
    {
2438
        // get skill
2439
        Skill::Skill skill_id = static_cast<Skill::Skill>(ss.id);
617,830 ✔
2440

2441
        // skip non-activation skills and Mimic (Mimic can't be mimicked)
2442
        if (!is_activation_skill(skill_id) || (skill_id == Skill::mimic))
892,085 ✔
2443
        { continue; }
343,575 ✔
2444

2445
        // skip mend for non-assault mimickers
2446
        if ((skill_id == Skill::mend || skill_id == Skill::fortify) && (src->m_card->m_type != CardType::assault))
274,255 ✔
2447
        { continue; }
1,200 ✔
2448

2449
        mimickable_skills.emplace_back(&ss);
273,055 ✔
2450
        _DEBUG_MSG(2, "  + %s\n", skill_description(fd->cards, ss).c_str());
617,830 ✔
2451
    }
2452
    }
2453

2454
    // select skill
2455
    unsigned mim_idx = 0;
206,333 ✔
2456
    switch (mimickable_skills.size())
206,333 ✔
2457
    {
2458
        case 0: assert(false); break;
×
2459
        case 1: break;
2460
        default: mim_idx = (fd->re() % mimickable_skills.size()); break;
64,977 ✔
2461
    }
2462
    // prepare & perform selected skill
2463
    const SkillSpec & mim_ss = *mimickable_skills[mim_idx];
206,333 ✔
2464
    Skill::Skill mim_skill_id = static_cast<Skill::Skill>(mim_ss.id);
206,333 ✔
2465
    auto skill_value = s.x + src->enhanced(mim_skill_id); //enhanced skill from mimic ?!?
206,333 ✔
2466
    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,};
206,333 ✔
2467
    _DEBUG_MSG(1, " * Mimicked skill: %s\n", skill_description(fd->cards, mimicked_ss).c_str());
206,333 ✔
2468
    skill_table[mim_skill_id](fd, src, mimicked_ss);
206,333 ✔
2469
}
206,333 ✔
2470

2471
    template<unsigned skill_id>
2472
inline unsigned select_fast(Field* fd, CardStatus* src, const std::vector<CardStatus*>& cards, const SkillSpec& s)
131,481,087 ✔
2473
{
2474
    if ((s.y == allfactions)
131,481,087 ✔
2475
            || fd->bg_effects[fd->tapi][PassiveBGE::metamorphosis]
20,608,548 ✔
2476
            || fd->bg_effects[fd->tapi][PassiveBGE::megamorphosis])
151,934,437 ✔
2477
    {
2478
        auto pred = [fd, src, s](CardStatus* c) {
426,024,473 ✔
2479
            return(skill_predicate<skill_id>(fd, src, c, s));
370,958,576 ✔
2480
        };
2481
        return fd->make_selection_array(cards.begin(), cards.end(), pred);
111,183,035 ✔
2482
    }
2483
    else
2484
    {
2485
        auto pred = [fd, src, s](CardStatus* c) {
107,493,549 ✔
2486
            return ((c->m_card->m_faction == s.y || c->m_card->m_faction == progenitor) && skill_predicate<skill_id>(fd, src, c, s));
101,940,324 ✔
2487
        };
2488
        return fd->make_selection_array(cards.begin(), cards.end(), pred);
20,298,052 ✔
2489
    }
2490
}
2491

2492
    template<>
2493
inline unsigned select_fast<Skill::mend>(Field* fd, CardStatus* src, const std::vector<CardStatus*>& cards, const SkillSpec& s)
109,071 ✔
2494
{
2495
    fd->selection_array.clear();
109,071 ✔
2496
    bool critical_reach = fd->bg_effects[fd->tapi][PassiveBGE::criticalreach];
109,071 ✔
2497
    auto& assaults = fd->players[src->m_player]->assaults;
109,071 ✔
2498
    unsigned adj_size = 1 + (unsigned)(critical_reach);
109,071 ✔
2499
    unsigned host_idx = src->m_index;
109,071 ✔
2500
    unsigned from_idx = safe_minus(host_idx, adj_size);
109,071 ✔
2501
    unsigned till_idx = std::min(host_idx + adj_size, safe_minus(assaults.size(), 1));
212,814 ✔
2502
    for (; from_idx <= till_idx; ++ from_idx)
374,753 ✔
2503
    {
2504
        if (from_idx == host_idx) { continue; }
267,324 ✔
2505
        CardStatus* adj_status = &assaults[from_idx];
156,611 ✔
2506
        if (!is_alive(adj_status)) { continue; }
156,611 ✔
2507
        if (skill_predicate<Skill::mend>(fd, src, adj_status, s))
309,938 ✔
2508
        {
2509
            fd->selection_array.push_back(adj_status);
4,380 ✔
2510
        }
2511
    }
2512
    return fd->selection_array.size();
109,071 ✔
2513
}
2514

2515
    template<>
2516
inline unsigned select_fast<Skill::fortify>(Field* fd, CardStatus* src, const std::vector<CardStatus*>& cards, const SkillSpec& s)
1,197,556 ✔
2517
{
2518
    fd->selection_array.clear();
1,197,556 ✔
2519
    bool critical_reach = fd->bg_effects[fd->tapi][PassiveBGE::criticalreach];
1,197,556 ✔
2520
    auto& assaults = fd->players[src->m_player]->assaults;
1,197,556 ✔
2521
    unsigned adj_size = 1 + (unsigned)(critical_reach);
1,197,556 ✔
2522
    unsigned host_idx = src->m_index;
1,197,556 ✔
2523
    unsigned from_idx = safe_minus(host_idx, adj_size);
1,197,556 ✔
2524
    unsigned till_idx = std::min(host_idx + adj_size, safe_minus(assaults.size(), 1));
2,353,241 ✔
2525
    for (; from_idx <= till_idx; ++ from_idx)
4,242,016 ✔
2526
    {
2527
        if (from_idx == host_idx) { continue; }
3,059,298 ✔
2528
        CardStatus* adj_status = &assaults[from_idx];
1,846,904 ✔
2529
        if (!is_alive(adj_status)) { continue; }
1,846,904 ✔
2530
        if (skill_predicate<Skill::fortify>(fd, src, adj_status, s))
1,832,066 ✔
2531
        {
2532
            fd->selection_array.push_back(adj_status);
1,832,066 ✔
2533
        }
2534
    }
2535
    return fd->selection_array.size();
1,197,556 ✔
2536
}
2537
inline std::vector<CardStatus*>& skill_targets_hostile_assault(Field* fd, CardStatus* src)
44,000,334 ✔
2538
{
2539
    return(fd->players[opponent(src->m_player)]->assaults.m_indirect);
44,000,334 ✔
2540
}
2541

2542
inline std::vector<CardStatus*>& skill_targets_allied_assault(Field* fd, CardStatus* src)
85,635,459 ✔
2543
{
2544
    return(fd->players[src->m_player]->assaults.m_indirect);
85,635,459 ✔
2545
}
2546

2547
inline std::vector<CardStatus*>& skill_targets_hostile_structure(Field* fd, CardStatus* src)
3,151,921 ✔
2548
{
2549
    return(fd->players[opponent(src->m_player)]->structures.m_indirect);
3,151,921 ✔
2550
}
2551

2552
inline std::vector<CardStatus*>& skill_targets_allied_structure(Field* fd, CardStatus* src)
2553
{
2554
    return(fd->players[src->m_player]->structures.m_indirect);
2555
}
2556

2557
    template<unsigned skill>
2558
std::vector<CardStatus*>& skill_targets(Field* fd, CardStatus* src)
2559
{
2560
    std::cerr << "skill_targets: Error: no specialization for " << skill_names[skill] << "\n";
2561
    throw;
2562
}
2563

2564
template<> std::vector<CardStatus*>& skill_targets<Skill::enfeeble>(Field* fd, CardStatus* src)
10,622,009 ✔
2565
{ return(skill_targets_hostile_assault(fd, src)); }
10,622,009 ✔
2566

2567
template<> std::vector<CardStatus*>& skill_targets<Skill::enhance>(Field* fd, CardStatus* src)
15,943,669 ✔
2568
{ return(skill_targets_allied_assault(fd, src)); }
15,943,669 ✔
2569

2570
template<> std::vector<CardStatus*>& skill_targets<Skill::evolve>(Field* fd, CardStatus* src)
1,649,113 ✔
2571
{ return(skill_targets_allied_assault(fd, src)); }
1,649,113 ✔
2572

2573
template<> std::vector<CardStatus*>& skill_targets<Skill::heal>(Field* fd, CardStatus* src)
25,490,769 ✔
2574
{ return(skill_targets_allied_assault(fd, src)); }
25,490,769 ✔
2575

2576
template<> std::vector<CardStatus*>& skill_targets<Skill::jam>(Field* fd, CardStatus* src)
3,891,586 ✔
2577
{ return(skill_targets_hostile_assault(fd, src)); }
3,891,586 ✔
2578

2579
template<> std::vector<CardStatus*>& skill_targets<Skill::mend>(Field* fd, CardStatus* src)
109,071 ✔
2580
{ return(skill_targets_allied_assault(fd, src)); }
109,071 ✔
2581

2582
template<> std::vector<CardStatus*>& skill_targets<Skill::fortify>(Field* fd, CardStatus* src)
1,197,556 ✔
2583
{ return(skill_targets_allied_assault(fd, src)); }
1,197,556 ✔
2584

2585
template<> std::vector<CardStatus*>& skill_targets<Skill::overload>(Field* fd, CardStatus* src)
162,774 ✔
2586
{ return(skill_targets_allied_assault(fd, src)); }
162,774 ✔
2587

2588
template<> std::vector<CardStatus*>& skill_targets<Skill::protect>(Field* fd, CardStatus* src)
13,537,414 ✔
2589
{ return(skill_targets_allied_assault(fd, src)); }
13,537,414 ✔
2590

2591
template<> std::vector<CardStatus*>& skill_targets<Skill::rally>(Field* fd, CardStatus* src)
8,367,689 ✔
2592
{ return(skill_targets_allied_assault(fd, src)); }
8,367,689 ✔
2593

2594
template<> std::vector<CardStatus*>& skill_targets<Skill::enrage>(Field* fd, CardStatus* src)
13,992,233 ✔
2595
{ return(skill_targets_allied_assault(fd, src)); }
13,992,233 ✔
2596

2597
template<> std::vector<CardStatus*>& skill_targets<Skill::entrap>(Field* fd, CardStatus* src)
5,185,171 ✔
2598
{ return(skill_targets_allied_assault(fd, src)); }
5,185,171 ✔
2599

2600
template<> std::vector<CardStatus*>& skill_targets<Skill::rush>(Field* fd, CardStatus* src)
×
2601
{ return(skill_targets_allied_assault(fd, src)); }
×
2602

2603
template<> std::vector<CardStatus*>& skill_targets<Skill::siege>(Field* fd, CardStatus* src)
3,151,921 ✔
2604
{ return(skill_targets_hostile_structure(fd, src)); }
3,151,921 ✔
2605

2606
template<> std::vector<CardStatus*>& skill_targets<Skill::strike>(Field* fd, CardStatus* src)
14,462,545 ✔
2607
{ return(skill_targets_hostile_assault(fd, src)); }
615,287 ✔
2608

2609
template<> std::vector<CardStatus*>& skill_targets<Skill::sunder>(Field* fd, CardStatus* src)
5,575,453 ✔
2610
{ return(skill_targets_hostile_assault(fd, src)); }
5,575,453 ✔
2611

2612
template<> std::vector<CardStatus*>& skill_targets<Skill::weaken>(Field* fd, CardStatus* src)
9,125,680 ✔
2613
{ return(skill_targets_hostile_assault(fd, src)); }
9,125,680 ✔
2614

2615
template<> std::vector<CardStatus*>& skill_targets<Skill::mimic>(Field* fd, CardStatus* src)
323,061 ✔
2616
{ return(skill_targets_hostile_assault(fd, src)); }
323,061 ✔
2617

2618
    template<Skill::Skill skill_id>
2619
inline bool check_and_perform_skill(Field* fd, CardStatus* src, CardStatus* dst, const SkillSpec& s, bool is_evadable
145,317,313 ✔
2620
#ifndef NQUEST
2621
        , bool & has_counted_quest
2622
#endif
2623
        )
2624
{
2625
    if (__builtin_expect(skill_check<skill_id>(fd, dst, src), true))
145,317,313 ✔
2626
    {
2627
#ifndef NQUEST
2628
        if (src->m_player == 0 && ! has_counted_quest)
2629
        {
2630
            fd->inc_counter(QuestType::skill_use, skill_id, dst->m_card->m_id);
2631
            has_counted_quest = true;
2632
        }
2633
#endif
2634
        if (is_evadable && (dst->m_evaded < dst->skill(Skill::evade)))
145,317,313 ✔
2635
        {
2636
            ++ dst->m_evaded;
17,094,598 ✔
2637
            _DEBUG_MSG(1, "%s %s on %s but it evades\n",
19,055,626 ✔
2638
                    status_description(src).c_str(), skill_short_description(fd->cards, s).c_str(),
2639
                    status_description(dst).c_str());
2640
            return(false);
17,094,598 ✔
2641
        }
2642
        _DEBUG_MSG(1, "%s %s on %s\n",
154,119,579 ✔
2643
                status_description(src).c_str(), skill_short_description(fd->cards, s).c_str(),
2644
                status_description(dst).c_str());
2645
        perform_skill<skill_id>(fd, src, dst, s);
128,222,715 ✔
2646
        if (s.c > 0)
128,222,715 ✔
2647
        {
2648
            src->m_skill_cd[skill_id] = s.c;
1,574,809 ✔
2649
        }
2650
        // Skill: Tribute
2651
        if (skill_check<Skill::tribute>(fd, dst, src)
149,968,223 ✔
2652
                // only activation helpful skills can be tributed (* except Evolve, Enhance, and Rush)
2653
                && is_activation_helpful_skill(s.id) && (s.id != Skill::evolve) && (s.id != Skill::enhance) && (s.id != Skill::rush)
21,745,508 ✔
2654
                && (dst->m_tributed < dst->skill(Skill::tribute))
8,328,044 ✔
2655
                && skill_check<skill_id>(fd, src, src))
114,540,158 ✔
2656
        {
2657
            ++ dst->m_tributed;
969,212 ✔
2658
            _DEBUG_MSG(1, "%s tributes %s back to %s\n",
969,212 ✔
2659
                    status_description(dst).c_str(), skill_short_description(fd->cards, s).c_str(),
2660
                    status_description(src).c_str());
2661
            perform_skill<skill_id>(fd, src, src, s);
969,212 ✔
2662
        }
2663
        return(true);
128,222,715 ✔
2664
    }
2665
    _DEBUG_MSG(1, "(CANCELLED) %s %s on %s\n",
×
2666
            status_description(src).c_str(), skill_short_description(fd->cards, s).c_str(),
2667
            status_description(dst).c_str());
2668
    return(false);
2669
}
2670
template<enum CardType::CardType def_cardtype>
2671
void perform_bge_devour(Field* fd, CardStatus* att_status, CardStatus* def_status)
18,964,700 ✔
2672
{
2673
// Passive BGE: Devour
2674
                unsigned leech_value;
2675
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::devour], false)
11,911,030 ✔
2676
                        && ((leech_value = att_status->skill(Skill::leech) + att_status->skill(Skill::refresh)) > 0)
47,589 ✔
2677
                        && (def_cardtype == CardType::assault))
11,911,030 ✔
2678
                {
2679
                    unsigned bge_denominator = (fd->bg_effects[fd->tapi][PassiveBGE::devour] > 0)
1,878 ✔
2680
                        ? (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::devour]
939 ✔
2681
                        : 4;
2682
                    unsigned bge_value = (leech_value - 1) / bge_denominator + 1;
939 ✔
2683
                    if (! att_status->m_sundered)
939 ✔
2684
                    {
2685
                        _DEBUG_MSG(1, "Devour: %s gains %u attack\n",
2,775 ✔
2686
                                status_description(att_status).c_str(), bge_value);
2687
                        att_status->m_perm_attack_buff += bge_value;
925 ✔
2688
                    }
2689
                    _DEBUG_MSG(1, "Devour: %s extends max hp / heals itself for %u\n",
2,817 ✔
2690
                            status_description(att_status).c_str(), bge_value);
2691
                    att_status->ext_hp(bge_value);
939 ✔
2692
                }
2693
}
11,911,030 ✔
2694
template<enum CardType::CardType def_cardtype>
2695
void perform_bge_heroism(Field* fd, CardStatus* att_status, CardStatus* def_status)
18,964,700 ✔
2696
{
2697
// Passive BGE: Heroism
2698
                unsigned valor_value;
2699
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::heroism], false)
18,964,700 ✔
2700
                        && ((valor_value = att_status->skill(Skill::valor) + att_status->skill(Skill::bravery)) > 0)
23,749 ✔
2701
                        && !att_status->m_sundered
2,471 ✔
2702
                        && (def_cardtype == CardType::assault) && (def_status->m_hp <= 0))
11,913,489 ✔
2703
                {
2704
                    _DEBUG_MSG(1, "Heroism: %s gain %u attack\n",
2,211 ✔
2705
                            status_description(att_status).c_str(), valor_value);
2706
                    att_status->m_perm_attack_buff += valor_value;
737 ✔
2707
                }
2708
                }
11,911,030 ✔
2709
/**
2710
 * @brief Perform Mark skill, increases Mark-counter.
2711
 * 
2712
 * @param fd     Field
2713
 * @param att_status Attacker
2714
 * @param def_status Defender
2715
 */
2716
void perform_mark(Field* fd, CardStatus* att_status, CardStatus* def_status)
18,964,700 ✔
2717
{
2718
    // Bug fix? 2023-04-03 mark should come after berserk
2719
    // Increase Mark-counter
2720
    unsigned mark_base = att_status->skill(Skill::mark);
18,964,700 ✔
2721
    if(mark_base && skill_check<Skill::mark>(fd,att_status,def_status)) {
18,964,700 ✔
2722
        _DEBUG_MSG(1, "%s marks %s for %u\n",
35,405 ✔
2723
                status_description(att_status).c_str(), status_description(def_status).c_str(), mark_base);
35,405 ✔
2724
        def_status->m_marked += mark_base;
35,405 ✔
2725
    }
2726
}
18,964,700 ✔
2727
/**
2728
 * @brief Perform Berserk skill and Passive BGE: EnduringRage
2729
 * 
2730
 * Increases attack by berserk value if not sundered.
2731
 * 
2732
 * @param fd     Field
2733
 * @param att_status Attacker
2734
 * @param def_status Defender
2735
 */
2736
void perform_berserk(Field* fd, CardStatus* att_status, CardStatus* def_status)
18,964,700 ✔
2737
{
2738
            // Skill: Berserk
2739
            unsigned berserk_value = att_status->skill(Skill::berserk);
18,964,700 ✔
2740
            if ( !att_status->m_sundered && (berserk_value > 0))
18,964,700 ✔
2741
            {
2742
                // perform_skill_berserk
2743
                att_status->m_perm_attack_buff += berserk_value;
7,485,992 ✔
2744
#ifndef NQUEST
2745
                if (att_status->m_player == 0)
2746
                {
2747
                    fd->inc_counter(QuestType::skill_use, Skill::berserk);
2748
                }
2749
#endif
2750

2751
                // Passive BGE: EnduringRage
2752
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::enduringrage], false))
7,485,992 ✔
2753
                {
2754
                    unsigned bge_denominator = (fd->bg_effects[fd->tapi][PassiveBGE::enduringrage] > 0)
21,673 ✔
2755
                        ? (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::enduringrage]
21,673 ✔
2756
                        : 2;
21,673 ✔
2757
                    unsigned bge_value = (berserk_value - 1) / bge_denominator + 1;
21,673 ✔
2758
                    _DEBUG_MSG(1, "EnduringRage: %s heals and protects itself for %u\n",
21,673 ✔
2759
                            status_description(att_status).c_str(), bge_value);
21,673 ✔
2760
                    att_status->add_hp(bge_value);
21,673 ✔
2761
                    att_status->m_protected += bge_value;
21,673 ✔
2762
                }
2763
            }
2764

2765

2766
}
18,964,700 ✔
2767
/**
2768
 * @brief Poisoning of a attacking card
2769
 * 
2770
 * @param fd  Field
2771
 * @param att_status Attacking card
2772
 * @param def_status Defending card
2773
 */
2774
void perform_poison(Field* fd, CardStatus* att_status, CardStatus* def_status)
23,073,533 ✔
2775
{
2776
    if(__builtin_expect(fd->fixes[Fix::poison_after_attacked],true))
23,073,533 ✔
2777
    {
2778
        if (is_alive(att_status) && def_status->has_skill(Skill::poison))
23,073,533 ✔
2779
        {
2780
            unsigned poison_value = def_status->skill(Skill::poison);
10,140 ✔
2781
            _DEBUG_MSG(1, "%s gets poisoned by %u from %s\n",
10,140 ✔
2782
                            status_description(att_status).c_str(), poison_value,
2783
                            status_description(def_status).c_str());
10,140 ✔
2784
            att_status->m_poisoned += poison_value; 
10,140 ✔
2785
        }
2786
    }
2787
}
23,073,533 ✔
2788

2789
void perform_corrosive(Field* fd, CardStatus* att_status, CardStatus* def_status)
23,073,534 ✔
2790
{
2791
    // Skill: Corrosive
2792
    unsigned corrosive_value = def_status->skill(Skill::corrosive);
23,073,534 ✔
2793
    if (corrosive_value > att_status->m_corroded_rate)
23,073,534 ✔
2794
    {
2795
        // perform_skill_corrosive
2796
        _DEBUG_MSG(1, "%s corrodes %s by %u\n",
45,166 ✔
2797
                status_description(def_status).c_str(),
2798
                status_description(att_status).c_str(), corrosive_value);
45,166 ✔
2799
        att_status->m_corroded_rate = corrosive_value;
45,166 ✔
2800
    }
2801
}
23,073,534 ✔
2802

2803
template<enum CardType::CardType def_cardtype>
2804
void perform_counter(Field* fd, CardStatus* att_status, CardStatus* def_status)
23,798,519 ✔
2805
{
2806
            // Enemy Skill: Counter
2807
            if (def_status->has_skill(Skill::counter))
23,798,519 ✔
2808
            {
2809
                // perform_skill_counter
2810
                unsigned counter_dmg(counter_damage(fd, att_status, def_status));
2,104,947 ✔
2811
#ifndef NQUEST
2812
                if (def_status->m_player == 0)
2813
                {
2814
                    fd->inc_counter(QuestType::skill_use, Skill::counter);
2815
                    fd->inc_counter(QuestType::skill_damage, Skill::counter, 0, counter_dmg);
2816
                }
2817
#endif
2818
                _DEBUG_MSG(1, "%s takes %u counter damage from %s\n",
3,163,095 ✔
2819
                        status_description(att_status).c_str(), counter_dmg,
2820
                        status_description(def_status).c_str());
2821
                remove_hp(fd, att_status, counter_dmg);
2,104,947 ✔
2822
                prepend_on_death(fd);
2,104,947 ✔
2823
                resolve_skill(fd);
2,104,947 ✔
2824

2825
                // Passive BGE: Counterflux
2826
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::counterflux], false)
2,104,947 ✔
2827
                        && (def_cardtype == CardType::assault) && is_alive(def_status))
1,255,718 ✔
2828
                {
2829
                    unsigned flux_denominator = (fd->bg_effects[fd->tapi][PassiveBGE::counterflux] > 0)
8,294 ✔
2830
                        ? (unsigned)fd->bg_effects[fd->tapi][PassiveBGE::counterflux]
4,147 ✔
2831
                        : 4;
2832
                    unsigned flux_value = (def_status->skill(Skill::counter) - 1) / flux_denominator + 1;
4,147 ✔
2833
                    _DEBUG_MSG(1, "Counterflux: %s heals itself and berserks for %u\n",
12,441 ✔
2834
                            status_description(def_status).c_str(), flux_value);
2835
                    def_status->add_hp(flux_value);
4,147 ✔
2836
                    if (! def_status->m_sundered)
4,147 ✔
2837
                    { def_status->m_perm_attack_buff += flux_value; }
3,785 ✔
2838
                }
2839
            }
2840
}
23,798,519 ✔
2841
bool check_and_perform_enhance(Field* fd, CardStatus* src, bool early)
172,915,678 ✔
2842
{
2843
      if(!is_active(src))return false; // active
172,915,678 ✔
2844
      if(!src->has_skill(Skill::enhance))return false; // enhance Skill
107,174,100 ✔
2845
      for(auto ss : src->m_card->m_skills)
126,805,632 ✔
2846
      {
2847
          if(ss.id != Skill::enhance)continue;
95,104,074 ✔
2848
          if(early ^ (ss.s == Skill::allegiance || ss.s == Skill::absorb ||ss.s == Skill::stasis || ss.s == Skill::bravery))continue; //only specified skills are 'early'
32,227,932 ✔
2849
          skill_table[ss.id](fd,src,ss);
15,850,779 ✔
2850
      }
2851
      return true;
31,701,558 ✔
2852
}
2853
bool check_and_perform_early_enhance(Field* fd, CardStatus* src)
86,457,839 ✔
2854
{
2855
      return check_and_perform_enhance(fd,src,true);
86,457,839 ✔
2856
}
2857
bool check_and_perform_later_enhance(Field* fd, CardStatus* src)
86,457,839 ✔
2858
{
2859
      return check_and_perform_enhance(fd,src,false);
86,457,839 ✔
2860
}
2861
/**
2862
 * @brief Perform drain skill
2863
 * 
2864
 * @param fd Field
2865
 * @param att_status Attacker status
2866
 * @param def_status Defender status
2867
 * @param att_dmg damage dealt by attacker
2868
 */
2869
template<>
2870
void PerformAttack::perform_swipe_drain<CardType::assault>(Field* fd, CardStatus* att_status, CardStatus* def_status,unsigned att_dmg) {
16,250,732 ✔
2871
        unsigned swipe_value = att_status->skill(Skill::swipe);
16,250,732 ✔
2872
        unsigned drain_value = att_status->skill(Skill::drain);
16,250,732 ✔
2873
        if (swipe_value || drain_value)
16,250,732 ✔
2874
        {
2875
            Storage<CardStatus>& def_assaults(fd->tip->assaults);
1,699,671 ✔
2876
            bool critical_reach = fd->bg_effects[fd->tapi][PassiveBGE::criticalreach];
1,699,671 ✔
2877
            auto drain_total_dmg = att_dmg;
1,699,671 ✔
2878
            unsigned adj_size = 1 + (unsigned)(critical_reach);
1,699,671 ✔
2879
            unsigned host_idx = def_status->m_index;
1,699,671 ✔
2880
            unsigned from_idx = safe_minus(host_idx, adj_size);
1,699,671 ✔
2881
            unsigned till_idx = std::min(host_idx + adj_size, safe_minus(def_assaults.size(), 1));
2,858,104 ✔
2882
            for (; from_idx <= till_idx; ++ from_idx)
4,713,691 ✔
2883
            {
2884
                if (from_idx == host_idx) { continue; }
3,014,020 ✔
2885
                CardStatus* adj_status = &def_assaults[from_idx];
1,314,349 ✔
2886
                if (!is_alive(adj_status)) { continue; }
1,314,349 ✔
2887
                _DEBUG_ASSERT(adj_status->m_card->m_type == CardType::assault); //only assaults
1,061,879 ✔
2888
                //unsigned swipe_dmg = safe_minus(
2889
                //    swipe_value + drain_value + def_status->m_enfeebled,
2890
                //    def_status->protected_value());
2891
                unsigned remaining_dmg = remove_absorption(fd,adj_status,swipe_value + drain_value + adj_status->m_enfeebled);
1,061,879 ✔
2892
                remaining_dmg = safe_minus(remaining_dmg,adj_status->protected_value());
1,061,879 ✔
2893
                _DEBUG_MSG(1, "%s swipes %s for %u damage\n",
1,061,879 ✔
2894
                        status_description(att_status).c_str(),
2895
                        status_description(adj_status).c_str(), remaining_dmg);
1,061,879 ✔
2896

2897
                remove_hp(fd, adj_status, remaining_dmg);
1,061,879 ✔
2898
                drain_total_dmg += remaining_dmg;
1,061,879 ✔
2899
            }
2900
            if (drain_value && skill_check<Skill::drain>(fd, att_status, nullptr))
1,699,671 ✔
2901
            {
2902
                _DEBUG_MSG(1, "%s drains %u hp\n",
68,403 ✔
2903
                        status_description(att_status).c_str(), drain_total_dmg);
68,403 ✔
2904
                att_status->add_hp(drain_total_dmg);
68,403 ✔
2905
            }
2906
            prepend_on_death(fd);
1,699,671 ✔
2907
            resolve_skill(fd);
1,699,671 ✔
2908
        }
2909
}
16,250,732 ✔
2910
/**
2911
 * @brief Perform Hunt skill
2912
 * 
2913
 * @param fd Field 
2914
 * @param att_status Attacker status
2915
 * @param def_status Defender status
2916
 */
2917
void perform_hunt(Field* fd, CardStatus* att_status, CardStatus* def_status) {
23,810,349 ✔
2918
    unsigned hunt_value = att_status->skill(Skill::hunt);
23,810,349 ✔
2919
        if(hunt_value)
23,810,349 ✔
2920
        {
2921
            CardStatus* hunted_status{select_first_enemy_assault(fd)};
32,361 ✔
2922
            if (hunted_status != nullptr)
32,361 ✔
2923
            {
2924
                unsigned remaining_dmg = remove_absorption(fd,hunted_status,hunt_value + hunted_status->m_enfeebled);
22,349 ✔
2925
                remaining_dmg = safe_minus(remaining_dmg,hunted_status->protected_value());
22,349 ✔
2926
                _DEBUG_MSG(1, "%s hunts %s for %u damage\n",
22,349 ✔
2927
                        status_description(att_status).c_str(),
2928
                        status_description(hunted_status).c_str(), remaining_dmg);
22,349 ✔
2929

2930
                remove_hp(fd, hunted_status, remaining_dmg);
22,349 ✔
2931

2932
                prepend_on_death(fd);
22,349 ✔
2933
                resolve_skill(fd);
22,349 ✔
2934
            }
2935
        }
2936
}
23,810,349 ✔
2937
/**
2938
 * @brief Perform Subdue skill
2939
 * 
2940
 * @param fd Field
2941
 * @param att_status Attacker status
2942
 * @param def_status Defender status
2943
 */
2944
void perform_subdue(Field* fd, CardStatus* att_status, CardStatus* def_status)
25,336,531 ✔
2945
{
2946
                // Skill: Subdue
2947
                unsigned subdue_value = def_status->skill(Skill::subdue);
25,336,531 ✔
2948
                if (__builtin_expect(subdue_value, false))
25,336,531 ✔
2949
                {
2950
                    _DEBUG_MSG(1, "%s subdues %s by %u\n",
997,165 ✔
2951
                            status_description(def_status).c_str(),
2952
                            status_description(att_status).c_str(), subdue_value);
997,165 ✔
2953
                    att_status->m_subdued += subdue_value;
997,165 ✔
2954
                    //fix negative attack
2955
                    if(att_status->calc_attack_power()<0)
997,165 ✔
2956
                    {
2957
                        att_status->m_temp_attack_buff -= att_status->calc_attack_power();
3,906 ✔
2958
                    }
2959
                    if (att_status->m_hp > att_status->max_hp())
1,125,465 ✔
2960
                    {
2961
                        _DEBUG_MSG(1, "%s loses %u HP due to subdue (max hp: %u)\n",
163,302 ✔
2962
                                status_description(att_status).c_str(),
2963
                                (att_status->m_hp - att_status->max_hp()),
2964
                                att_status->max_hp());
163,302 ✔
2965
                        att_status->m_hp = att_status->max_hp();
288,369 ✔
2966
                    }
2967
                }
2968
}
25,336,531 ✔
2969
bool check_and_perform_valor(Field* fd, CardStatus* src)
15,656,497 ✔
2970
{
2971
    unsigned valor_value = src->skill(Skill::valor);
15,656,497 ✔
2972
    if (valor_value && !src->m_sundered && skill_check<Skill::valor>(fd, src, nullptr))
31,312,994 ✔
2973
    {
2974
        _DEBUG_ASSERT(src->m_card->m_type == CardType::assault); //only assaults
45,974 ✔
2975
        unsigned opponent_player = opponent(src->m_player);
45,974 ✔
2976
        const CardStatus * dst = fd->players[opponent_player]->assaults.size() > src->m_index ?
45,974 ✔
2977
            &fd->players[opponent_player]->assaults[src->m_index] :
20,510 ✔
2978
            nullptr;
45,974 ✔
2979
        if (dst == nullptr || dst->m_hp <= 0)
20,510 ✔
2980
        {
2981
            _DEBUG_MSG(1, "%s loses Valor (no blocker)\n", status_description(src).c_str());
25,464 ✔
2982
            return false;
25,464 ✔
2983
        }
2984
        else if (dst->attack_power() <= src->attack_power())
20,510 ✔
2985
        {
2986
            _DEBUG_MSG(1, "%s loses Valor (weak blocker %s)\n", status_description(src).c_str(), status_description(dst).c_str());
2,201 ✔
2987
            return false;
2,201 ✔
2988
        }
2989
#ifndef NQUEST
2990
        if (src->m_player == 0)
2991
        {
2992
            fd->inc_counter(QuestType::skill_use, Skill::valor);
2993
        }
2994
#endif
2995
        _DEBUG_MSG(1, "%s activates Valor %u\n", status_description(src).c_str(), valor_value);
18,309 ✔
2996
        src->m_perm_attack_buff += valor_value;
18,309 ✔
2997
        return true;
18,309 ✔
2998
    }
2999
    return false;
3000
}
3001

3002
bool check_and_perform_bravery(Field* fd, CardStatus* src)
67,042,401 ✔
3003
{
3004
    unsigned bravery_value = src->skill(Skill::bravery);
67,042,401 ✔
3005
    if (bravery_value && !src->m_sundered && skill_check<Skill::bravery>(fd, src, nullptr))
134,084,802 ✔
3006
    {
3007
        _DEBUG_ASSERT(src->m_card->m_type == CardType::assault); //only assaults
2,191,091 ✔
3008
        unsigned opponent_player = opponent(src->m_player);
2,191,091 ✔
3009
        const CardStatus * dst = fd->players[opponent_player]->assaults.size() > src->m_index ?
2,191,091 ✔
3010
            &fd->players[opponent_player]->assaults[src->m_index] :
1,221,293 ✔
3011
            nullptr;
2,191,091 ✔
3012
        if (dst == nullptr || dst->m_hp <= 0)
1,221,293 ✔
3013
        {
3014
            _DEBUG_MSG(1, "%s loses Bravery (no blocker)\n", status_description(src).c_str());
969,798 ✔
3015
            return false;
969,798 ✔
3016
        }
3017
        else if (dst->attack_power() <= src->attack_power())
1,221,293 ✔
3018
        {
3019
            _DEBUG_MSG(1, "%s loses Bravery (weak blocker %s)\n", status_description(src).c_str(), status_description(dst).c_str());
898,522 ✔
3020
            return false;
898,522 ✔
3021
        }
3022
#ifndef NQUEST
3023
        if (src->m_player == 0)
3024
        {
3025
            fd->inc_counter(QuestType::skill_use, Skill::bravery);
3026
        }
3027
#endif
3028
        _DEBUG_MSG(1, "%s activates Bravery %u\n", status_description(src).c_str(), bravery_value);
322,771 ✔
3029
        src->m_perm_attack_buff += bravery_value;
322,771 ✔
3030

3031
        //BGE: superheroism
3032
        if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::superheroism], false))
322,771 ✔
3033
        {
3034
            unsigned bge_value = bravery_value * fd->bg_effects[fd->tapi][PassiveBGE::superheroism];
994 ✔
3035
            const SkillSpec ss_heal{Skill::heal, bge_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, true, 0,};
994 ✔
3036
            _DEBUG_MSG(1, "%s activates SuperHeroism: %s\n", status_description(src).c_str(),
994 ✔
3037
                    skill_description(fd->cards, ss_heal).c_str());
994 ✔
3038
            //fd->skill_queue.emplace(fd->skill_queue.begin()+0, src, ss_heal);
3039
            skill_table[Skill::heal](fd,src,ss_heal); //Probably better to perform the skill direct instead of queuing it
994 ✔
3040
        }
3041

3042
        return true;
322,771 ✔
3043
    }
3044
    return false;
3045
}
3046

3047
bool check_and_perform_inhibit(Field* fd, CardStatus* att_status,CardStatus* def_status)
36,721,577 ✔
3048
{
3049
      unsigned inhibit_value = att_status->skill(Skill::inhibit);
36,721,577 ✔
3050
      if (inhibit_value > def_status->m_inhibited && skill_check<Skill::inhibit>(fd, att_status, def_status))
39,945,105 ✔
3051
      {
3052
        _DEBUG_MSG(1, "%s inhibits %s by %u\n",
3,223,527 ✔
3053
                status_description(att_status).c_str(),
3054
                status_description(def_status).c_str(), inhibit_value);
3,223,527 ✔
3055
        def_status->m_inhibited = inhibit_value;
3,223,527 ✔
3056
        return true;
3,223,527 ✔
3057
      }
3058
      return false;
3059
}
3060
bool check_and_perform_sabotage(Field* fd, CardStatus* att_status, CardStatus* def_status)
36,721,577 ✔
3061
{
3062
    unsigned sabotage_value = att_status->skill(Skill::sabotage);
36,721,577 ✔
3063
    if (sabotage_value > def_status->m_sabotaged && skill_check<Skill::sabotage>(fd, att_status, def_status))
37,562,036 ✔
3064
    {
3065
        _DEBUG_MSG(1, "%s sabotages %s by %u\n",
840,459 ✔
3066
                status_description(att_status).c_str(),
3067
                status_description(def_status).c_str(), sabotage_value);
840,459 ✔
3068
        def_status->m_sabotaged = sabotage_value;
840,459 ✔
3069
        return true;
840,459 ✔
3070
    }
3071
    return false;
3072
}
3073
bool check_and_perform_disease(Field* fd, CardStatus* att_status,CardStatus* def_status)
36,721,577 ✔
3074
{
3075
    unsigned disease_base = att_status->skill(Skill::disease);
36,721,577 ✔
3076
    if(disease_base && skill_check<Skill::disease>(fd, att_status, def_status)) {
36,980,555 ✔
3077
        _DEBUG_MSG(1, "%s diseases %s for %u\n",
258,978 ✔
3078
        status_description(att_status).c_str(), status_description(def_status).c_str(), disease_base);
258,978 ✔
3079
        def_status->m_diseased += disease_base;
258,978 ✔
3080
        return true;
258,978 ✔
3081
    }
3082
    return false;
3083
}
3084

3085
CardStatus* check_and_perform_summon(Field* fd, CardStatus* src)
14,721,435 ✔
3086
{
3087
    unsigned summon_card_id = src->m_card->m_skill_value[Skill::summon];
14,721,435 ✔
3088
    if (summon_card_id)
14,721,435 ✔
3089
    {
3090
        const Card* summoned_card(fd->cards.by_id(summon_card_id));
3,547,592 ✔
3091
        _DEBUG_MSG(1, "%s summons %s\n", status_description(src).c_str(), summoned_card->m_name.c_str());
3,547,592 ✔
3092
        CardStatus* summoned_status = nullptr;
3,547,592 ✔
3093
        switch (summoned_card->m_type)
3,547,592 ✔
3094
        {
3095
            case CardType::assault:
1,196,171 ✔
3096
                summoned_status = PlayCard(summoned_card, fd, src->m_player, src).op<CardType::assault>(true);
1,196,171 ✔
3097
                return summoned_status;
1,196,171 ✔
3098
            case CardType::structure:
2,351,421 ✔
3099
                summoned_status = PlayCard(summoned_card, fd, src->m_player, src).op<CardType::structure>(true);
2,351,421 ✔
3100
                return summoned_status;
2,351,421 ✔
3101
            default:
×
3102
                _DEBUG_MSG(0, "Unknown card type: #%u %s: %u\n",
×
3103
                        summoned_card->m_id, card_description(fd->cards, summoned_card).c_str(),
3104
                        summoned_card->m_type);
×
3105
                _DEBUG_ASSERT(false);
×
3106
                __builtin_unreachable();
3107
        }
3108
    }
3109
    return nullptr;
3110
}
3111

3112

3113
    template<Skill::Skill skill_id>
3114
size_t select_targets(Field* fd, CardStatus* tsrc, const SkillSpec& s)
132,172,427 ✔
3115
{
3116
    size_t n_candidates;
3117
    CardStatus* src;
3118
    if(fd->fixes[Fix::revenge_on_death] && s.s2 == Skill::revenge)
132,172,427 ✔
3119
    {
3120
            _DEBUG_MSG(2,"FIX ON DEATH REVENGE SELECTION")
34,738 ✔
3121
            src = &fd->players[(tsrc->m_player+1)%2]->commander; // selection like enemy commander
34,738 ✔
3122
    }
34,738 ✔
3123
    else
3124
    {
3125
            src = tsrc;
3126
    }
3127
    switch (skill_id)
3128
    {
3129
        case Skill::mortar:
2,924,455 ✔
3130
            n_candidates = select_fast<Skill::siege>(fd, src, skill_targets<Skill::siege>(fd, src), s);
2,924,455 ✔
3131
            if (n_candidates == 0)
2,924,455 ✔
3132
            {
3133
                n_candidates = select_fast<Skill::strike>(fd, src, skill_targets<Skill::strike>(fd, src), s);
615,287 ✔
3134
            }
3135
            break;
3136

3137
        default:
129,247,972 ✔
3138
            n_candidates = select_fast<skill_id>(fd, src, skill_targets<skill_id>(fd, src), s);
129,247,972 ✔
3139
            break;
3140
    }
3141

3142
    // (false-loop)
3143
    unsigned n_selected = n_candidates;
132,660,469 ✔
3144
    do
132,172,427 ✔
3145
    {
3146
        // no candidates
3147
        if (n_candidates == 0)
129,863,259 ✔
3148
        { break; }
3149

3150
        // show candidates (debug)
3151
        _DEBUG_SELECTION("%s", skill_names[skill_id].c_str());
80,817,137 ✔
3152

3153
        // analyze targets count / skill
3154
        unsigned n_targets = s.n > 0 ? s.n : 1;
79,659,531 ✔
3155
        if (s.all || n_targets >= n_candidates || skill_id == Skill::mend || skill_id == Skill::fortify)  // target all or mend
79,659,531 ✔
3156
        { break; }
3157

3158
        // shuffle & trim
3159
        for (unsigned i = 0; i < n_targets; ++i)
42,161,402 ✔
3160
        {
3161
            std::swap(fd->selection_array[i], fd->selection_array[fd->rand(i, n_candidates - 1)]);
21,293,983 ✔
3162
        }
3163
        fd->selection_array.resize(n_targets);
20,867,419 ✔
3164
        if (n_targets > 1)
20,867,419 ✔
3165
        {
3166
            std::sort(fd->selection_array.begin(), fd->selection_array.end(),
299,023 ✔
3167
                    [](const CardStatus * a, const CardStatus * b) { return a->m_index < b->m_index; });
703,826 ✔
3168
        }
3169
        n_selected = n_targets;
3170

3171
    } while (false); // (end)
3172

3173
    return n_selected;
132,172,427 ✔
3174
}
3175

3176
    template<Skill::Skill skill_id>
3177
void perform_targetted_allied_fast(Field* fd, CardStatus* src, const SkillSpec& s)
85,629,636 ✔
3178
{
3179
    select_targets<skill_id>(fd, src, s);
85,629,636 ✔
3180
    unsigned num_inhibited = 0;
85,629,636 ✔
3181
#ifndef NQUEST
3182
    bool has_counted_quest = false;
3183
#endif
3184
    bool src_overloaded = src->m_overloaded;
85,629,636 ✔
3185
    // take the targets without copying them (nested skills select into fd->selection_array), give the buffer back after
3186
    std::vector<CardStatus*> selection_array;
85,629,636 ✔
3187
    selection_array.swap(fd->selection_array);
85,629,636 ✔
3188
    for (CardStatus * dst: selection_array)
178,632,299 ✔
3189
    {
3190
        if (dst->m_inhibited > 0 && !src_overloaded)
93,002,663 ✔
3191
        {
3192
            _DEBUG_MSG(1, "%s %s on %s but it is inhibited\n",
6,062,535 ✔
3193
                    status_description(src).c_str(), skill_short_description(fd->cards, s).c_str(),
3194
                    status_description(dst).c_str());
3195
            -- dst->m_inhibited;
5,600,515 ✔
3196
            ++ num_inhibited;
5,600,515 ✔
3197
            continue;
5,600,515 ✔
3198
        }
5,600,515 ✔
3199
        check_and_perform_skill<skill_id>(fd, src, dst, s, false
87,402,148 ✔
3200
#ifndef NQUEST
3201
                , has_counted_quest
3202
#endif
3203
                );
3204
    }
3205
    fd->selection_array.swap(selection_array);
85,629,636 ✔
3206

3207
    // Passive BGE: Divert
3208
    if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::divert], false)
85,629,636 ✔
3209
            && (num_inhibited > 0))
85,629,636 ✔
3210
    {
3211
        SkillSpec diverted_ss = s;
5,773 ✔
3212
        diverted_ss.y = allfactions;
5,773 ✔
3213
        diverted_ss.n = 1;
5,773 ✔
3214
        diverted_ss.all = false;
5,773 ✔
3215
        for (unsigned i = 0; i < num_inhibited; ++ i)
11,596 ✔
3216
        {
3217
            select_targets<skill_id>(fd, &fd->tip->commander, diverted_ss);
5,823 ✔
3218
            std::vector<CardStatus*> selection_array = fd->selection_array;
5,823 ✔
3219
            for (CardStatus * dst: selection_array)
9,808 ✔
3220
            {
3221
                if (dst->m_inhibited > 0)
3,985 ✔
3222
                {
3223
                    _DEBUG_MSG(1, "%s %s (Diverted) on %s but it is inhibited\n",
687 ✔
3224
                            status_description(src).c_str(), skill_short_description(fd->cards, diverted_ss).c_str(),
3225
                            status_description(dst).c_str());
3226
                    -- dst->m_inhibited;
229 ✔
3227
                    continue;
229 ✔
3228
                }
229 ✔
3229
                _DEBUG_MSG(1, "%s %s (Diverted) on %s\n",
11,268 ✔
3230
                        status_description(src).c_str(), skill_short_description(fd->cards, diverted_ss).c_str(),
3231
                        status_description(dst).c_str());
3232
                perform_skill<skill_id>(fd, src, dst, diverted_ss);
3,778 ✔
3233
            }
3234
        }
3235
    }
3236
}
85,629,636 ✔
3237

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

3255
    template<Skill::Skill skill_id>
3256
void perform_targetted_hostile_fast(Field* fd, CardStatus* src, const SkillSpec& s)
46,536,968 ✔
3257
{
3258
    select_targets<skill_id>(fd, src, s);
46,536,968 ✔
3259
    std::vector<CardStatus *> paybackers;
46,536,968 ✔
3260
#ifndef NQUEST
3261
    bool has_counted_quest = false;
3262
#endif
3263
    const bool has_turningtides = (fd->bg_effects[fd->tapi][PassiveBGE::turningtides] && (skill_id == Skill::weaken || skill_id == Skill::sunder));
46,536,968 ✔
3264
    unsigned turningtides_value(0), old_attack(0);
46,536,968 ✔
3265

3266
    // apply skill to each target(dst)
3267
    unsigned selection_array_len = fd->selection_array.size();
46,536,968 ✔
3268
    // take the targets without copying them (nested skills select into fd->selection_array), give the buffer back after
3269
    std::vector<CardStatus*> selection_array;
46,536,968 ✔
3270
    selection_array.swap(fd->selection_array);
46,536,968 ✔
3271
    for (CardStatus * dst: selection_array)
104,435,024 ✔
3272
    {
3273
        // TurningTides
3274
        if (__builtin_expect(has_turningtides, false))
16,203,437 ✔
3275
        {
3276
            old_attack = dst->attack_power();
11,323 ✔
3277
        }
3278

3279
        // check & apply skill to target(dst)
3280
        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))
58,104,389 ✔
3281
#ifndef NQUEST
3282
                    , has_counted_quest
3283
#endif
3284
                    ))
3285
        {
3286
            // TurningTides: get max attack decreasing
3287
            if (__builtin_expect(has_turningtides, false))
10,814,884 ✔
3288
            {
3289
                turningtides_value = std::max(turningtides_value, safe_minus(old_attack, dst->attack_power()));
26,038 ✔
3290
            }
3291

3292
            // Payback/Revenge: collect paybackers/revengers
3293
            unsigned payback_value = dst->skill(Skill::payback) + dst->skill(Skill::revenge);
40,803,458 ✔
3294
            if ((s.id != Skill::mimic) && (dst->m_paybacked < payback_value) && skill_check<Skill::payback>(fd, dst, src))
40,803,458 ✔
3295
            {
3296
                paybackers.reserve(selection_array_len);
1,060,351 ✔
3297
                paybackers.push_back(dst);
1,060,351 ✔
3298
            }
3299
        }
3300
    }
3301
    fd->selection_array.swap(selection_array);
46,536,968 ✔
3302

3303
    // apply TurningTides
3304
    if (__builtin_expect(has_turningtides, false) && (turningtides_value > 0))
14,701,133 ✔
3305
    {
3306
        SkillSpec ss_rally{Skill::rally, turningtides_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, s.all, 0,};
8,509 ✔
3307
        _DEBUG_MSG(1, "TurningTides %u!\n", turningtides_value);
17,018 ✔
3308
        perform_targetted_allied_fast<Skill::rally>(fd, &fd->players[src->m_player]->commander, ss_rally);
8,509 ✔
3309
    }
3310

3311
    prepend_on_death(fd);  // skills
46,536,968 ✔
3312

3313
    // Payback/Revenge
3314
    for (CardStatus * pb_status: paybackers)
47,597,319 ✔
3315
    {
3316
        turningtides_value = 0;
1,060,351 ✔
3317

3318
        // apply Revenge
3319
        if (pb_status->skill(Skill::revenge))
1,060,351 ✔
3320
        {
3321
            unsigned revenged_count(0);
3322
            for (unsigned case_index(0); case_index < 3; ++ case_index)
4,239,364 ✔
3323
            {
3324
                CardStatus * target_status;
3325
#ifndef NDEBUG
3326
                const char * target_desc;
3327
#endif
3328
                switch (case_index)
3,179,523 ✔
3329
                {
3330
                    // revenge to left
3331
                    case 0:
1,059,841 ✔
3332
                        if (!(target_status = fd->left_assault(src))) { continue; }
279,402 ✔
3333
#ifndef NDEBUG
3334
                        target_desc = "left";
3335
#endif
3336
                        break;
3337

3338
                        // revenge to core
3339
                    case 1:
3340
                        target_status = src;
3341
#ifndef NDEBUG
3342
                        target_desc = "core";
3343
#endif
3344
                        break;
3345

3346
                        // revenge to right
3347
                    case 2:
1,059,841 ✔
3348
                        if (!(target_status = fd->right_assault(src))) { continue; }
2,431,666 ✔
3349
#ifndef NDEBUG
3350
                        target_desc = "right";
3351
#endif
3352
                        break;
3353

3354
                        // wtf?
3355
                    default:
3356
                        __builtin_unreachable();
3357
                }
3358

3359
                // skip illegal target
3360
                if (!skill_predicate<skill_id>(fd, target_status, target_status, s))
2,653,812 ✔
3361
                {
3362
                    continue;
162,886 ✔
3363
                }
3364

3365
                // skip dead target
3366
                if (!is_alive(target_status))
2,490,926 ✔
3367
                {
3368
#ifndef NDEBUG
3369
                    _DEBUG_MSG(1, "(CANCELLED: target unit dead) %s Revenge (to %s) %s on %s\n",
×
3370
                            status_description(pb_status).c_str(), target_desc,
3371
                            skill_short_description(fd->cards, s).c_str(), status_description(target_status).c_str());
3372
#endif
3373
                    continue;
×
3374
                }
×
3375

3376
                // TurningTides
3377
                if (__builtin_expect(has_turningtides, false))
94,374 ✔
3378
                {
3379
                    old_attack = target_status->attack_power();
839 ✔
3380
                }
3381

3382
                // apply revenged skill
3383
#ifndef NDEBUG
3384
                _DEBUG_MSG(1, "%s Revenge (to %s) %s on %s\n",
2,680,450 ✔
3385
                        status_description(pb_status).c_str(), target_desc,
3386
                        skill_short_description(fd->cards, s).c_str(), status_description(target_status).c_str());
3387
#endif
3388
                perform_skill<skill_id>(fd, pb_status, target_status, s);
2,490,926 ✔
3389
                ++ revenged_count;
2,490,926 ✔
3390

3391
                // revenged TurningTides: get max attack decreasing
3392
                if (__builtin_expect(has_turningtides, false))
94,374 ✔
3393
                {
3394
                    turningtides_value = std::max(turningtides_value, safe_minus(old_attack, target_status->attack_power()));
2,203 ✔
3395
                }
3396
            }
3397
            if (revenged_count)
1,059,841 ✔
3398
            {
3399
                // consume remaining payback/revenge
3400
                ++ pb_status->m_paybacked;
1,038,554 ✔
3401

3402
                // apply TurningTides
3403
                if (__builtin_expect(has_turningtides, false) && (turningtides_value > 0))
49,690 ✔
3404
                {
3405
                    SkillSpec ss_rally{Skill::rally, turningtides_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
446 ✔
3406
                    _DEBUG_MSG(1, "Paybacked TurningTides %u!\n", turningtides_value);
892 ✔
3407
                    perform_targetted_allied_fast<Skill::rally>(fd, &fd->players[pb_status->m_player]->commander, ss_rally);
446 ✔
3408
                }
3409
            }
3410
        }
3411
        // apply Payback
3412
        else
3413
        {
3414
            // skip illegal target(src)
3415
            if (!skill_predicate<skill_id>(fd, src, src, s))
510 ✔
3416
            {
3417
                continue;
170 ✔
3418
            }
3419

3420
            // skip dead target(src)
3421
            if (!is_alive(src))
340 ✔
3422
            {
3423
                _DEBUG_MSG(1, "(CANCELLED: src unit dead) %s Payback %s on %s\n",
×
3424
                        status_description(pb_status).c_str(), skill_short_description(fd->cards, s).c_str(),
3425
                        status_description(src).c_str());
3426
                continue;
×
3427
            }
×
3428

3429
            // TurningTides
3430
            if (__builtin_expect(has_turningtides, false))
107 ✔
3431
            {
3432
                old_attack = src->attack_power();
×
3433
            }
3434

3435
            // apply paybacked skill
3436
            _DEBUG_MSG(1, "%s Payback %s on %s\n",
1,020 ✔
3437
                    status_description(pb_status).c_str(), skill_short_description(fd->cards, s).c_str(), status_description(src).c_str());
3438
            perform_skill<skill_id>(fd, pb_status, src, s);
340 ✔
3439
            ++ pb_status->m_paybacked;
340 ✔
3440

3441
            // handle paybacked TurningTides
3442
            if (__builtin_expect(has_turningtides, false))
107 ✔
3443
            {
3444
                turningtides_value = std::max(turningtides_value, safe_minus(old_attack, src->attack_power()));
×
3445
                if (turningtides_value > 0)
×
3446
                {
3447
                    SkillSpec ss_rally{Skill::rally, turningtides_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
×
3448
                    _DEBUG_MSG(1, "Paybacked TurningTides %u!\n", turningtides_value);
×
3449
                    perform_targetted_allied_fast<Skill::rally>(fd, &fd->players[pb_status->m_player]->commander, ss_rally);
×
3450
                }
3451
            }
3452
        }
3453
    }
3454

3455
    prepend_on_death(fd,true);  // paybacked skills
46,536,968 ✔
3456
}
46,536,968 ✔
3457

3458
//------------------------------------------------------------------------------
3459
inline unsigned evaluate_brawl_score(Field* fd, unsigned player)
×
3460
{
3461
    const auto & p = fd->players;
×
3462
    return 55
×
3463
        // - (10 - p[player]->deck->cards.size())
3464
        // + (10 - p[opponent(player)]->deck->cards.size())
3465
        + p[opponent(player)]->total_cards_destroyed
×
3466
        + p[player]->deck->shuffled_cards.size()
×
3467
        - (unsigned)((fd->turn+7)/8);
×
3468
}
3469

3470
inline unsigned evaluate_war_score(Field* fd, unsigned player)
×
3471
{
3472
    return 208 - ((unsigned)(fd->turn)/2)*4;
×
3473
}
3474
int evaluate_card(Field* fd,const Card* cs);
3475
int evaluate_skill(Field* fd,const Card* c , SkillSpec* ss)
×
3476
{
3477
        // TODO optimize this
3478
        int tvalue = ss->x;
×
3479

3480
        if(ss->card_id != 0)tvalue += 1*evaluate_card(fd,card_by_id_safe(fd->cards,ss->card_id));
×
3481
        tvalue += 10*(ss->id==Skill::flurry);
×
3482
        tvalue += 10*(ss->id==Skill::jam);
×
3483
        tvalue += 5*(ss->id==Skill::overload);
×
3484
        tvalue += 2*(ss->id==Skill::flying);
×
3485
        tvalue += 2*(ss->id==Skill::evolve);
×
3486
        tvalue += 2*(ss->id==Skill::wall);
×
3487
        tvalue += 5*(ss->id==Skill::tribute);
×
3488

3489
        tvalue *= 1.+1.5*(ss->id==Skill::flurry);
×
3490
        tvalue *= 1.+1.5*(ss->id==Skill::drain);
×
3491
        tvalue *= 1.+1.5*(ss->id==Skill::mortar);
×
3492
        tvalue *= 1.+1.5*(ss->id==Skill::scavenge);
×
3493
        tvalue *= 1.+1.5*(ss->id==Skill::disease);
×
3494

3495
        tvalue *= 1.+1.3*(ss->id==Skill::rally);
×
3496
        tvalue *= 1.+1.3*(ss->id==Skill::strike);
×
3497

3498
        tvalue *= 1.+1.2*(ss->id==Skill::avenge);
×
3499
        tvalue *= 1.+1.1*(ss->id==Skill::sunder);
×
3500
        tvalue *= 1.+1.1*(ss->id==Skill::venom);
×
3501

3502
        tvalue *= 1.+1.0*(ss->id==Skill::evade);
×
3503
        tvalue *= 1.+1.0*(ss->id==Skill::enfeeble);
×
3504

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

3507
        tvalue *= 1.+0.2*(ss->id==Skill::fortify);
×
3508
        tvalue *= 1.+0.5*(ss->id==Skill::mend);
×
3509

3510
        tvalue *= 1.+0.4*(ss->id==Skill::jam);
×
3511
        tvalue *= 1.+0.4*(ss->id==Skill::overload);
×
3512
        //tvalue *= 1.+0.4*(ss->id==Skill::rupture);
3513
        tvalue *= 1.+0.4*(ss->id==Skill::bravery);
×
3514
        tvalue *= 1.+0.4*(ss->id==Skill::entrap);
×
3515
        tvalue *= 1.+0.4*(ss->id==Skill::heal);
×
3516

3517

3518
        tvalue *= 1.+0.3*(ss->id==Skill::revenge);
×
3519
        tvalue *= 1.+0.3*(ss->id==Skill::enrage);
×
3520

3521

3522
        //tvalue *= 1.+2.1*(ss->id==Skill::hunt);
3523
        tvalue *= 1.+0.1*(ss->id==Skill::mark);
×
3524
        tvalue *= 1.+0.1*(ss->id==Skill::coalition);
×
3525
        tvalue *= 1.+0.1*(ss->id==Skill::legion);
×
3526
        //tvalue *= 1.+1.1*(ss->id==Skill::barrier);
3527
        tvalue *= 1.+0.1*(ss->id==Skill::pierce);
×
3528
        tvalue *= 1.+0.1*(ss->id==Skill::armor);
×
3529
        //tvalue *= 1.+0.1*(ss->id==Skill::swipe);
3530
        //tvalue *= 1.+0.1*(ss->id==Skill::berserk);
3531
        tvalue *= 1.-0.1*(ss->id==Skill::weaken);
×
3532

3533

3534

3535
        tvalue *= 1.-0.5 *(ss->id==Skill::sabotage); //sucks
×
3536
        tvalue *= 1.-0.5 *(ss->id==Skill::inhibit); //sucks
×
3537
        tvalue *= 1.-0.5 *(ss->id==Skill::corrosive); //sucks
×
3538
        tvalue *= 1.-0.5 *(ss->id==Skill::payback); //sucks
×
3539
        tvalue *= 1.-0.5 *(ss->id==Skill::leech); //sucks
×
3540

3541

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

3585
        int scale = is_alive(&fd->tap->commander);
×
3586
        auto& assaults(fd->tap->assaults);
×
3587
        auto& structures(fd->tap->structures);
×
3588

3589

3590
        value += 0.5*scale * evaluate_cardstatus(fd,&fd->tap->commander);
×
3591
        for(unsigned index(0); index < assaults.size();++index)
×
3592
        {
3593
                value += scale * evaluate_cardstatus(fd,&assaults[index]);
×
3594
        }
3595
        for(unsigned index(0); index < structures.size();++index)
×
3596
        {
3597
                value += scale * evaluate_cardstatus(fd,&structures[index]);
×
3598
        }
3599

3600
        scale = is_alive(&fd->tip->commander);
×
3601
        auto& eassaults(fd->tip->assaults);
×
3602
        auto& estructures(fd->tip->structures);
×
3603
        value -= 0.5*scale * evaluate_cardstatus(fd,&fd->tip->commander);
×
3604
        for(unsigned index(0); index < eassaults.size();++index)
×
3605
        {
3606
                value -= (scale * evaluate_cardstatus(fd,&eassaults[index]));
×
3607
        }
3608
        for(unsigned index(0); index < estructures.size();++index)
×
3609
        {
3610
                value -= (scale * evaluate_cardstatus(fd,&estructures[index]));
×
3611
        }
3612
        return value;
×
3613
}
3614

3615

3616
Results<uint64_t> evaluate_sim_result(Field* fd, bool single_turn_both)
1,541,549 ✔
3617
{
3618
    typedef unsigned points_score_type;
1,541,549 ✔
3619
    const auto & p = fd->players;
1,541,549 ✔
3620
    unsigned raid_damage = 0;
1,541,549 ✔
3621
#ifndef NQUEST
3622
    unsigned quest_score = 0;
3623
#endif
3624

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

3756
    // Huh? How did we get here?
3757
    assert(false);
×
3758
    return {0, 0, 0, 0,1};
3759
}
3760

3761
//------------------------------------------------------------------------------
3762
//turns_both sets the number of turns to sim before exiting before winner exists.
3763
Results<uint64_t> play(Field* fd,bool skip_init, bool skip_preplay,unsigned turns_both)
1,541,549 ✔
3764
{
3765
    if(!skip_init){ //>>> start skip init
1,541,549 ✔
3766
        fd->players[0]->commander.m_player = 0;
1,541,549 ✔
3767
        fd->players[1]->commander.m_player = 1;
1,541,549 ✔
3768
        fd->tapi = fd->gamemode == surge ? 1 : 0;
1,541,549 ✔
3769
        fd->tipi = opponent(fd->tapi);
1,541,549 ✔
3770
        fd->tap = fd->players[fd->tapi];
1,541,549 ✔
3771
        fd->tip = fd->players[fd->tipi];
1,541,549 ✔
3772
        fd->end = false;
1,541,549 ✔
3773

3774
        // Play dominion & fortresses
3775
        for (unsigned _(0), ai(fd->tapi); _ < 2; ++_)
4,624,647 ✔
3776
        {
3777
            if (fd->players[ai]->deck->alpha_dominion)
3,083,098 ✔
3778
            { PlayCard(fd->players[ai]->deck->alpha_dominion, fd, ai, &fd->players[ai]->commander).op<CardType::structure>(); }
1,663,627 ✔
3779
            for (const Card* played_card: fd->players[ai]->deck->shuffled_forts)
6,166,196 ✔
3780
            {
3781

3782
                switch (played_card->m_type)
×
3783
                {
3784
                    case CardType::assault:
×
3785
                        PlayCard(played_card, fd, ai, &fd->players[ai]->commander).op<CardType::assault>();
×
3786
                        break;
×
3787
                    case CardType::structure:
×
3788
                        PlayCard(played_card, fd, ai, &fd->players[ai]->commander).op<CardType::structure>();
×
3789
                        break;
×
3790
                    case CardType::commander:
×
3791
                    case CardType::num_cardtypes:
×
3792
                        _DEBUG_MSG(0, "Unknown card type: #%u %s: %u\n",
×
3793
                                played_card->m_id, card_description(fd->cards, played_card).c_str(), played_card->m_type);
×
3794
                        assert(false);
×
3795
                        break;
3796
                }
3797
            }
3798
            resolve_skill(fd);
3,083,098 ✔
3799
            std::swap(fd->tapi, fd->tipi);
3,083,098 ✔
3800
            std::swap(fd->tap, fd->tip);
3,083,098 ✔
3801
            ai = opponent(ai);
3,083,098 ✔
3802
        }
3803
    }//>>> end skip init
3804
    unsigned both_turn_limit = fd->turn+2*turns_both;
1,541,549 ✔
3805
    while(__builtin_expect(fd->turn <= turn_limit && !fd->end && (turns_both==0 || fd->turn < both_turn_limit), true))
21,113,321 ✔
3806
    {
3807
        if(!skip_preplay){ //>>> start skip init
21,092,461 ✔
3808

3809
            fd->current_phase = Field::playcard_phase;
21,092,461 ✔
3810
            // Initialize stuff, remove dead cards
3811
            _DEBUG_MSG(1, "------------------------------------------------------------------------\n"
21,092,461 ✔
3812
                    "TURN %u begins for %s\n", fd->turn, status_description(&fd->tap->commander).c_str());
21,092,461 ✔
3813

3814
            // reduce timers & perform triggered skills (like Summon)
3815
            fd->prepare_action();
21,092,461 ✔
3816
            turn_start_phase(fd); // summon may postpone skills to be resolved
21,092,461 ✔
3817
            resolve_skill(fd); // resolve postponed skills recursively
21,092,461 ✔
3818
            fd->finalize_action();
21,092,461 ✔
3819

3820
            //bool bge_megamorphosis = fd->bg_effects[fd->tapi][PassiveBGE::megamorphosis];
3821

3822
        }//>>> end skip init
3823
        else { skip_preplay = false;}
3824
        // Play a card
3825
        const Card* played_card(fd->tap->deck->next(fd));
21,092,461 ✔
3826
        if (played_card)
21,092,461 ✔
3827
        {
3828

3829
            // Begin 'Play Card' phase action
3830
            fd->prepare_action();
19,174,366 ✔
3831

3832
            // Play selected card
3833
            //CardStatus* played_status = nullptr;
3834
            switch (played_card->m_type)
19,174,366 ✔
3835
            {
3836
                case CardType::assault:
18,262,390 ✔
3837
                    PlayCard(played_card, fd, fd->tapi, &fd->tap->commander).op<CardType::assault>();
18,262,390 ✔
3838
                    break;
18,262,390 ✔
3839
                case CardType::structure:
911,976 ✔
3840
                    PlayCard(played_card, fd, fd->tapi, &fd->tap->commander).op<CardType::structure>();
911,976 ✔
3841
                    break;
911,976 ✔
3842
                case CardType::commander:
×
3843
                case CardType::num_cardtypes:
×
3844
                    _DEBUG_MSG(0, "Unknown card type: #%u %s: %u\n",
×
3845
                            played_card->m_id, card_description(fd->cards, played_card).c_str(), played_card->m_type);
×
3846
                    assert(false);
×
3847
                    break;
3848
            }
3849
            resolve_skill(fd); // resolve postponed skills recursively
19,174,366 ✔
3850
            //status_description(played_status)
3851
            //_DEBUG_MSG(3,"Card played: %s", status_description(played_status).c_str());
3852
            // End 'Play Card' phase action
3853
            fd->finalize_action();
19,174,366 ✔
3854

3855

3856

3857
        }
3858
        if (__builtin_expect(fd->end, false)) { break; }
21,092,461 ✔
3859

3860
        //-------------------------------------------------
3861
        // Phase: (Later-) Enhance, Inhibit, Sabotage, Disease
3862
        //-------------------------------------------------
3863
        //Skill: Enhance
3864
        //Perform later enhance for commander
3865
        if(!fd->fixes[Fix::enhance_early]) {
21,092,461 ✔
3866
        check_and_perform_later_enhance(fd,&fd->tap->commander);
×
3867
        auto& structures(fd->tap->structures);
×
3868
        for(unsigned index(0); index < structures.size(); ++index)
×
3869
        {
3870
            CardStatus * status = &structures[index];
×
3871
            //enhance everything else after card was played
3872
            check_and_perform_later_enhance(fd,status);
×
3873
        }
3874
        }
3875
        //Perform Inhibit, Sabotage, Disease
3876
        auto& assaults(fd->tap->assaults);
21,092,461 ✔
3877
        for(unsigned index(0); index < assaults.size(); ++index)
81,543,395 ✔
3878
        {
3879
            CardStatus * att_status = &assaults[index];
60,450,934 ✔
3880
            if(att_status->m_index >= fd->tip->assaults.size())continue; //skip no enemy
60,450,934 ✔
3881
            auto def_status = &fd->tip->assaults[att_status->m_index];
36,721,774 ✔
3882
            if(!is_alive(def_status))continue; //skip dead
36,721,774 ✔
3883

3884
            check_and_perform_inhibit(fd,att_status,def_status);
36,721,577 ✔
3885
            check_and_perform_sabotage(fd,att_status,def_status);
36,721,577 ✔
3886
            check_and_perform_disease(fd,att_status,def_status);
36,721,577 ✔
3887
        }
3888
        //-------------------------------------------------
3889

3890
        // Evaluate Passive BGE Heroism skills
3891
        if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::heroism], false))
21,092,461 ✔
3892
        {
3893
            for (CardStatus * dst: fd->tap->assaults.m_indirect)
242,215 ✔
3894
            {
3895
                unsigned bge_value = (dst->skill(Skill::valor) + dst->skill(Skill::bravery)+ 1) / 2;
150,084 ✔
3896
                if (bge_value <= 0)
150,084 ✔
3897
                { continue; }
132,975 ✔
3898
                SkillSpec ss_protect{Skill::protect, bge_value, allfactions, 0, 0, Skill::no_skill, Skill::no_skill, false, 0,};
17,206 ✔
3899
                if (dst->m_inhibited > 0)
17,206 ✔
3900
                {
3901
                    _DEBUG_MSG(1, "Heroism: %s on %s but it is inhibited\n",
97 ✔
3902
                            skill_short_description(fd->cards, ss_protect).c_str(), status_description(dst).c_str());
97 ✔
3903
                    -- dst->m_inhibited;
97 ✔
3904

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

3944
        // Evaluate activation BGE skills
3945
        fd->current_phase = Field::bge_phase;
21,092,461 ✔
3946
        for (const auto & bg_skill: fd->bg_skills[fd->tapi])
21,185,163 ✔
3947
        {
3948
            fd->prepare_action();
92,702 ✔
3949
            _DEBUG_MSG(2, "Evaluating BG skill %s\n", skill_description(fd->cards, bg_skill).c_str());
92,702 ✔
3950
            fd->skill_queue.emplace_back(&fd->tap->commander, bg_skill);
92,702 ✔
3951
            resolve_skill(fd);
92,702 ✔
3952
            fd->finalize_action();
92,702 ✔
3953
        }
3954
        if (__builtin_expect(fd->end, false)) { break; }
21,092,461 ✔
3955

3956
        // Evaluate commander
3957
        fd->current_phase = Field::commander_phase;
21,092,461 ✔
3958
        evaluate_skills<CardType::commander>(fd, &fd->tap->commander, fd->tap->commander.m_card->m_skills);
21,092,461 ✔
3959
        if (__builtin_expect(fd->end, false)) { break; }
21,092,461 ✔
3960

3961
        // Evaluate structures
3962
        fd->current_phase = Field::structures_phase;
21,092,461 ✔
3963
        for (fd->current_ci = 0; !fd->end && (fd->current_ci < fd->tap->structures.size()); ++fd->current_ci)
48,639,507 ✔
3964
        {
3965
            CardStatus* current_status(&fd->tap->structures[fd->current_ci]);
27,547,046 ✔
3966
            if (!is_active(current_status))
27,547,046 ✔
3967
            {
3968
                _DEBUG_MSG(2, "%s cannot take action.\n", status_description(current_status).c_str());
9,351,547 ✔
3969
            }
3970
            else
3971
            {
3972
                evaluate_skills<CardType::structure>(fd, current_status, current_status->m_card->m_skills);
18,195,499 ✔
3973
            }
3974
        }
3975

3976
        // Evaluate assaults
3977
        fd->current_phase = Field::assaults_phase;
21,092,461 ✔
3978
        fd->bloodlust_value = 0;
21,092,461 ✔
3979
        for (fd->current_ci = 0; !fd->end && (fd->current_ci < fd->tap->assaults.size()); ++fd->current_ci)
75,541,604 ✔
3980
        {
3981
            CardStatus* current_status(&fd->tap->assaults[fd->current_ci]);
55,969,832 ✔
3982
            bool attacked = false;
55,969,832 ✔
3983
            if (!is_active(current_status))
55,969,832 ✔
3984
            {
3985
                _DEBUG_MSG(2, "%s cannot take action.\n", status_description(current_status).c_str());
28,842,983 ✔
3986
                // Passive BGE: HaltedOrders
3987
                /*
3988
                unsigned inhibit_value;
3989
                if (__builtin_expect(fd->bg_effects[fd->tapi][PassiveBGE::haltedorders], false)
3990
                        && (current_status->m_delay > 0) // still frozen
3991
                        && (fd->current_ci < fd->tip->assaults.size()) // across slot isn't empty
3992
                        && is_alive(&fd->tip->assaults[fd->current_ci]) // across assault is alive
3993
                        && ((inhibit_value = current_status->skill(Skill::inhibit))
3994
                            > fd->tip->assaults[fd->current_ci].m_inhibited)) // inhibit/re-inhibit(if higher)
3995
                        {
3996
                            CardStatus* across_status(&fd->tip->assaults[fd->current_ci]);
3997
                            _DEBUG_MSG(1, "Halted Orders: %s inhibits %s by %u\n",
3998
                                    status_description(current_status).c_str(),
3999
                                    status_description(across_status).c_str(), inhibit_value);
4000
                            across_status->m_inhibited = inhibit_value;
4001
                        }
4002
                        */
4003
            }
4004
            else
4005
            {
4006
                if (current_status->m_protected_stasis)
27,126,849 ✔
4007
                {
4008
                    _DEBUG_MSG(1, "%s loses Stasis protection (activated)\n",
2,427,846 ✔
4009
                            status_description(current_status).c_str());
4010
                }
4011
                current_status->m_protected_stasis = 0;
27,126,849 ✔
4012
                fd->assault_bloodlusted = false;
27,126,849 ✔
4013
                current_status->m_step = CardStep::attacking;
27,126,849 ✔
4014
                evaluate_skills<CardType::assault>(fd, current_status, current_status->m_card->m_skills, &attacked);
27,126,849 ✔
4015
                if (__builtin_expect(fd->end, false)) { break; }
27,126,849 ✔
4016
                if (__builtin_expect(!is_alive(current_status), false)) { continue; }
25,606,160 ✔
4017
            }
4018

4019
            current_status->m_step = CardStep::attacked;
53,699,877 ✔
4020
        }
4021
        fd->current_phase = Field::end_phase;
21,092,461 ✔
4022
        turn_end_phase(fd);
21,092,461 ✔
4023
        if (__builtin_expect(fd->end, false)) { break; }
21,092,461 ✔
4024
        _DEBUG_MSG(1, "TURN %u ends for %s\n", fd->turn, status_description(&fd->tap->commander).c_str());
19,571,772 ✔
4025
        std::swap(fd->tapi, fd->tipi);
19,571,772 ✔
4026
        std::swap(fd->tap, fd->tip);
19,571,772 ✔
4027
        ++fd->turn;
19,571,772 ✔
4028
    }
4029

4030
    return evaluate_sim_result(fd,turns_both!= 0);
1,541,549 ✔
4031
}
4032

4033
//------------------------------------------------------------------------------
4034
void fill_skill_table()
84 ✔
4035
{
4036
    memset(skill_table, 0, sizeof skill_table);
84 ✔
4037
    skill_table[Skill::mortar] = perform_targetted_hostile_fast<Skill::mortar>;
84 ✔
4038
    skill_table[Skill::enfeeble] = perform_targetted_hostile_fast<Skill::enfeeble>;
84 ✔
4039
    skill_table[Skill::enhance] = perform_targetted_allied_fast<Skill::enhance>;
84 ✔
4040
    skill_table[Skill::evolve] = perform_targetted_allied_fast<Skill::evolve>;
84 ✔
4041
    skill_table[Skill::heal] = perform_targetted_allied_fast<Skill::heal>;
84 ✔
4042
    skill_table[Skill::jam] = perform_targetted_hostile_fast<Skill::jam>;
84 ✔
4043
    skill_table[Skill::mend] = perform_targetted_allied_fast<Skill::mend>;
84 ✔
4044
    skill_table[Skill::fortify] = perform_targetted_allied_fast<Skill::fortify>;
84 ✔
4045
    skill_table[Skill::overload] = perform_targetted_allied_fast<Skill::overload>;
84 ✔
4046
    skill_table[Skill::protect] = perform_targetted_allied_fast<Skill::protect>;
84 ✔
4047
    skill_table[Skill::rally] = perform_targetted_allied_fast<Skill::rally>;
84 ✔
4048
    skill_table[Skill::enrage] = perform_targetted_allied_fast<Skill::enrage>;
84 ✔
4049
    skill_table[Skill::entrap] = perform_targetted_allied_fast<Skill::entrap>;
84 ✔
4050
    skill_table[Skill::rush] = perform_targetted_allied_fast_rush;
84 ✔
4051
    skill_table[Skill::siege] = perform_targetted_hostile_fast<Skill::siege>;
84 ✔
4052
    skill_table[Skill::strike] = perform_targetted_hostile_fast<Skill::strike>;
84 ✔
4053
    skill_table[Skill::sunder] = perform_targetted_hostile_fast<Skill::sunder>;
84 ✔
4054
    skill_table[Skill::weaken] = perform_targetted_hostile_fast<Skill::weaken>;
84 ✔
4055
    skill_table[Skill::mimic] = perform_targetted_hostile_fast<Skill::mimic>;
84 ✔
4056
}
84 ✔
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