Skip to content

Commit d5932eb

Browse files
committed
Support BGE aliases (data/bges.txt).
1 parent ef2da9c commit d5932eb

File tree

6 files changed

+169
-110
lines changed

6 files changed

+169
-110
lines changed

read.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,52 @@ void read_owned_cards(Cards& all_cards, std::map<unsigned, unsigned>& owned_card
478478
}
479479
}
480480

481+
unsigned read_bge_aliases(std::unordered_map<std::string, std::string> & bge_aliases, const std::string& filename)
482+
{
483+
if(!boost::filesystem::exists(filename))
484+
{
485+
return(0);
486+
}
487+
std::ifstream bgefile(filename);
488+
if(!bgefile.is_open())
489+
{
490+
std::cerr << "Error: BGE file " << filename << " could not be opened\n";
491+
return(2);
492+
}
493+
unsigned num_line(0);
494+
bgefile.exceptions(std::ifstream::badbit);
495+
try
496+
{
497+
while(bgefile && !bgefile.eof())
498+
{
499+
std::string bge_string;
500+
getline(bgefile, bge_string);
501+
++num_line;
502+
if(bge_string.size() == 0 || strncmp(bge_string.c_str(), "//", 2) == 0)
503+
{
504+
continue;
505+
}
506+
std::string bge_name;
507+
auto bge_string_iter = read_token(bge_string.begin(), bge_string.end(), [](char c){return(c == ':');}, bge_name);
508+
if(bge_string_iter == bge_string.end() || bge_name.empty())
509+
{
510+
std::cerr << "Error in BGE file " << filename << " at line " << num_line << ", could not read the name.\n";
511+
continue;
512+
}
513+
bge_string_iter = advance_until(bge_string_iter + 1, bge_string.end(), [](const char& c){return(c != ' ');});
514+
bge_aliases[simplify_name(bge_name)] = std::string{bge_string_iter, bge_string.end()};
515+
}
516+
}
517+
catch (std::exception& e)
518+
{
519+
std::cerr << "Exception while parsing the BGE file " << filename;
520+
if(num_line > 0)
521+
{
522+
std::cerr << " at line " << num_line;
523+
}
524+
std::cerr << ": " << e.what() << ".\n";
525+
return(3);
526+
}
527+
return(0);
528+
}
529+

read.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <map>
55
#include <string>
6-
#include <map>
76

87
#include "deck.h"
98

@@ -17,5 +16,6 @@ const std::pair<std::vector<unsigned>, std::map<signed, char>> string_to_ids(con
1716
unsigned load_custom_decks(Decks& decks, Cards& cards, const std::string & filename);
1817
void read_owned_cards(Cards& cards, std::map<unsigned, unsigned>& owned_cards, const std::string & filename);
1918
unsigned read_card_abbrs(Cards& cards, const std::string& filename);
19+
unsigned read_bge_aliases(std::unordered_map<std::string, std::string> & bge_aliases, const std::string & filename);
2020

2121
#endif

sim.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void prepend_on_death(Field* fd)
291291
}
292292
}
293293
// Virulence
294-
if (fd->bg_effects[opponent(status->m_player)].count(virulence))
294+
if (fd->bg_effects.count(virulence))
295295
{
296296
if (status->m_index != last_index + 1)
297297
{
@@ -329,10 +329,10 @@ void prepend_on_death(Field* fd)
329329
}
330330
}
331331
// Revenge
332-
if (fd->bg_effects[status->m_player].count(revenge))
332+
if (fd->bg_effects.count(revenge))
333333
{
334-
SkillSpec ss_heal{heal, fd->bg_effects[status->m_player].at(revenge), allfactions, 0, 0, no_skill, no_skill, true,};
335-
SkillSpec ss_rally{rally, fd->bg_effects[status->m_player].at(revenge), allfactions, 0, 0, no_skill, no_skill, true,};
334+
SkillSpec ss_heal{heal, fd->bg_effects.at(revenge), allfactions, 0, 0, no_skill, no_skill, true,};
335+
SkillSpec ss_rally{rally, fd->bg_effects.at(revenge), allfactions, 0, 0, no_skill, no_skill, true,};
336336
CardStatus * commander = &fd->players[status->m_player]->commander;
337337
_DEBUG_MSG(2, "Revenge: Preparing skill %s and %s\n", skill_description(fd->cards, ss_heal).c_str(), skill_description(fd->cards, ss_rally).c_str());
338338
od_skills.emplace_back(commander, ss_heal);
@@ -842,9 +842,9 @@ struct PerformAttack
842842
remove_hp(fd, att_status, counter_dmg);
843843
prepend_on_death(fd);
844844
resolve_skill(fd);
845-
if (def_cardtype == CardType::assault && def_status->m_hp > 0 && fd->bg_effects[def_status->m_player].count(counterflux))
845+
if (def_cardtype == CardType::assault && def_status->m_hp > 0 && fd->bg_effects.count(counterflux))
846846
{
847-
unsigned flux_denominator = fd->bg_effects[def_status->m_player].at(counterflux) ? fd->bg_effects[def_status->m_player].at(counterflux) : 4;
847+
unsigned flux_denominator = fd->bg_effects.at(counterflux) ? fd->bg_effects.at(counterflux) : 4;
848848
unsigned flux_value = (def_status->skill(counter) - 1) / flux_denominator + 1;
849849
_DEBUG_MSG(1, "Counterflux: %s heals itself and berserks for %u\n", status_description(def_status).c_str(), flux_value);
850850
add_hp(fd, def_status, flux_value);
@@ -868,9 +868,9 @@ struct PerformAttack
868868
{
869869
fd->inc_counter(QuestType::skill_use, berserk);
870870
}
871-
if (fd->bg_effects[att_status->m_player].count(enduringrage))
871+
if (fd->bg_effects.count(enduringrage))
872872
{
873-
unsigned bge_denominator = fd->bg_effects[att_status->m_player].at(enduringrage) ? fd->bg_effects[att_status->m_player].at(enduringrage) : 2;
873+
unsigned bge_denominator = fd->bg_effects.at(enduringrage) ? fd->bg_effects.at(enduringrage) : 2;
874874
unsigned bge_value = (berserk_value - 1) / bge_denominator + 1;
875875
_DEBUG_MSG(1, "EnduringRage: %s heals and protects itself for %u\n", status_description(att_status).c_str(), bge_value);
876876
add_hp(fd, att_status, bge_value);
@@ -879,7 +879,7 @@ struct PerformAttack
879879
}
880880
do_leech<def_cardtype>();
881881
unsigned valor_value = att_status->skill(valor);
882-
if (valor_value > 0 && ! att_status->m_sundered && fd->bg_effects[att_status->m_player].count(heroism) && def_cardtype == CardType::assault && def_status->m_hp <= 0)
882+
if (valor_value > 0 && ! att_status->m_sundered && fd->bg_effects.count(heroism) && def_cardtype == CardType::assault && def_status->m_hp <= 0)
883883
{
884884
_DEBUG_MSG(1, "Heroism: %s gain %u attack\n", status_description(att_status).c_str(), valor_value);
885885
att_status->m_attack += valor_value;
@@ -939,7 +939,7 @@ struct PerformAttack
939939
std::string reduced_desc;
940940
unsigned reduced_dmg(0);
941941
unsigned armor_value = def_status->skill(armor);
942-
if (def_status->m_card->m_type == CardType::assault && fd->bg_effects[def_status->m_player].count(fortification))
942+
if (def_status->m_card->m_type == CardType::assault && fd->bg_effects.count(fortification))
943943
{
944944
for (auto && adj_status: fd->adjacent_assaults(def_status))
945945
{
@@ -969,7 +969,7 @@ struct PerformAttack
969969
if(!desc.empty()) { desc += "=" + to_string(att_dmg); }
970970
_DEBUG_MSG(1, "%s attacks %s for %u%s damage\n", status_description(att_status).c_str(), status_description(def_status).c_str(), pre_modifier_dmg, desc.c_str());
971971
}
972-
if (legion_value > 0 && can_be_healed(att_status) && fd->bg_effects[att_status->m_player].count(brigade))
972+
if (legion_value > 0 && can_be_healed(att_status) && fd->bg_effects.count(brigade))
973973
{
974974
_DEBUG_MSG(1, "Brigade: %s heals itself for %u\n", status_description(att_status).c_str(), legion_value);
975975
add_hp(fd, att_status, legion_value);
@@ -1098,9 +1098,9 @@ bool attack_phase(Field* fd)
10981098
att_dmg = attack_commander(fd, att_status);
10991099
}
11001100

1101-
if (att_dmg > 0 && !fd->assault_bloodlusted && fd->bg_effects[fd->tapi].count(bloodlust))
1101+
if (att_dmg > 0 && !fd->assault_bloodlusted && fd->bg_effects.count(bloodlust))
11021102
{
1103-
fd->bloodlust_value += fd->bg_effects[fd->tapi].at(bloodlust);
1103+
fd->bloodlust_value += fd->bg_effects.at(bloodlust);
11041104
fd->assault_bloodlusted = true;
11051105
}
11061106

@@ -1336,7 +1336,7 @@ inline void perform_skill<weaken>(Field* fd, CardStatus* src, CardStatus* dst, c
13361336
template<unsigned skill_id>
13371337
inline unsigned select_fast(Field* fd, CardStatus* src, const std::vector<CardStatus*>& cards, const SkillSpec& s)
13381338
{
1339-
if (s.y == allfactions || fd->bg_effects[src->m_player].count(metamorphosis))
1339+
if (s.y == allfactions || fd->bg_effects.count(metamorphosis))
13401340
{
13411341
return(fd->make_selection_array(cards.begin(), cards.end(), [fd, src, s](CardStatus* c){return(skill_predicate<skill_id>(fd, src, c, s));}));
13421342
}
@@ -1563,7 +1563,7 @@ void perform_targetted_allied_fast(Field* fd, CardStatus* src, const SkillSpec&
15631563
}
15641564
check_and_perform_skill<skill_id>(fd, src, dst, s, false, has_counted_quest);
15651565
}
1566-
if (num_inhibited > 0 && fd->bg_effects[opponent(src->m_player)].count(divert))
1566+
if (num_inhibited > 0 && fd->bg_effects.count(divert))
15671567
{
15681568
SkillSpec diverted_ss = s;
15691569
diverted_ss.y = allfactions;
@@ -1589,6 +1589,11 @@ void perform_targetted_allied_fast(Field* fd, CardStatus* src, const SkillSpec&
15891589

15901590
void perform_targetted_allied_fast_rush(Field* fd, CardStatus* src, const SkillSpec& s)
15911591
{
1592+
if (src->m_card->m_type == CardType::commander)
1593+
{ // BGE skills are casted as by commander
1594+
perform_targetted_allied_fast<rush>(fd, src, s);
1595+
return;
1596+
}
15921597
if (src->m_rush_attempted)
15931598
{
15941599
_DEBUG_MSG(2, "%s does not check Rush again.\n", status_description(src).c_str());
@@ -1605,7 +1610,7 @@ void perform_targetted_hostile_fast(Field* fd, CardStatus* src, const SkillSpec&
16051610
select_targets<skill_id>(fd, src, s);
16061611
bool has_counted_quest = false;
16071612
std::vector<CardStatus *> paybackers;
1608-
if (fd->bg_effects[src->m_player].count(turningtides) && skill_id == weaken)
1613+
if (fd->bg_effects.count(turningtides) && skill_id == weaken)
16091614
{
16101615
unsigned turningtides_value = 0;
16111616
for (CardStatus * dst: fd->selection_array)
@@ -1717,8 +1722,8 @@ Results<uint64_t> play(Field* fd)
17171722
}
17181723
if(__builtin_expect(fd->end, false)) { break; }
17191724

1720-
// Evaluate Heroism Battleground skills
1721-
if (fd->bg_effects[fd->tapi].count(heroism))
1725+
// Evaluate Heroism BGE skills
1726+
if (fd->bg_effects.count(heroism))
17221727
{
17231728
for (CardStatus * dst: fd->tap->assaults.m_indirect)
17241729
{
@@ -1730,7 +1735,7 @@ Results<uint64_t> play(Field* fd)
17301735
{
17311736
_DEBUG_MSG(1, "Heroism: %s on %s but it is inhibited\n", skill_short_description(ss_protect).c_str(), status_description(dst).c_str());
17321737
-- dst->m_inhibited;
1733-
if (fd->bg_effects[fd->tipi].count(divert))
1738+
if (fd->bg_effects.count(divert))
17341739
{
17351740
SkillSpec diverted_ss = ss_protect;
17361741
diverted_ss.y = allfactions;
@@ -1759,7 +1764,7 @@ Results<uint64_t> play(Field* fd)
17591764
}
17601765
}
17611766

1762-
// Evaluate activation Battleground skills
1767+
// Evaluate activation BGE skills
17631768
for (const auto & bg_skill: fd->bg_skills[fd->tapi])
17641769
{
17651770
_DEBUG_MSG(2, "Evaluating BG skill %s\n", skill_description(fd->cards, bg_skill).c_str());

sim.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class Field
239239
gamemode_t gamemode;
240240
OptimizationMode optimization_mode;
241241
const Quest quest;
242-
std::unordered_map<unsigned, unsigned> bg_effects[2]; // passive BGE
242+
std::unordered_map<unsigned, unsigned> bg_effects; // passive BGE
243243
std::vector<SkillSpec> bg_skills[2]; // active BGE, casted every turn
244244
// With the introduction of on death skills, a single skill can trigger arbitrary many skills.
245245
// They are stored in this, and cleared after all have been performed.
@@ -266,7 +266,7 @@ class Field
266266
unsigned quest_counter;
267267

268268
Field(std::mt19937& re_, const Cards& cards_, Hand& hand1, Hand& hand2, gamemode_t gamemode_, OptimizationMode optimization_mode_, const Quest & quest_,
269-
std::unordered_map<unsigned, unsigned>& your_bg_effects_, std::unordered_map<unsigned, unsigned>& enemy_bg_effects_, std::vector<SkillSpec>& your_bg_skills_, std::vector<SkillSpec>& enemy_bg_skills_) :
269+
std::unordered_map<unsigned, unsigned>& bg_effects_, std::vector<SkillSpec>& your_bg_skills_, std::vector<SkillSpec>& enemy_bg_skills_) :
270270
end{false},
271271
re(re_),
272272
cards(cards_),
@@ -275,7 +275,7 @@ class Field
275275
gamemode(gamemode_),
276276
optimization_mode(optimization_mode_),
277277
quest(quest_),
278-
bg_effects{your_bg_effects_, enemy_bg_effects_},
278+
bg_effects{bg_effects_},
279279
bg_skills{your_bg_skills_, enemy_bg_skills_},
280280
assault_bloodlusted(false),
281281
bloodlust_value(0),

tyrant.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#ifndef TYRANT_H_INCLUDED
22
#define TYRANT_H_INCLUDED
33

4-
#define TYRANT_OPTIMIZER_VERSION "2.17.2"
4+
#define TYRANT_OPTIMIZER_VERSION "2.18.0"
55

66
#include <string>
77
#include <sstream>
88
#include <unordered_set>
9+
#include <unordered_map>
910
#include <tuple>
1011

1112
enum Faction

0 commit comments

Comments
 (0)