forked from andor9/tyrant_optimize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.h
More file actions
286 lines (258 loc) · 7.1 KB
/
sim.h
File metadata and controls
286 lines (258 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef SIM_H_INCLUDED
#define SIM_H_INCLUDED
#include <boost/pool/pool.hpp>
#include <string>
#include <array>
#include <deque>
#include <tuple>
#include <vector>
#include <map>
#include <random>
#include "tyrant.h"
class Card;
class Cards;
class Deck;
class Field;
class Achievement;
extern unsigned turn_limit;
inline unsigned safe_minus(unsigned x, unsigned y)
{
return(x - std::min(x, y));
}
//---------------------- Represent Simulation Results ----------------------------
template<typename result_type>
struct Results
{
result_type wins;
result_type draws;
result_type losses;
result_type points;
result_type sq_points;
template<typename other_result_type>
Results& operator+=(const Results<other_result_type>& other)
{
wins += other.wins;
draws += other.draws;
losses += other.losses;
points += other.points;
sq_points += other.points * other.points;
return *this;
}
};
typedef std::pair<std::vector<Results<uint64_t>>, unsigned> EvaluatedResults;
template<typename result_type>
struct FinalResults
{
result_type wins;
result_type draws;
result_type losses;
//MDJ
result_type wins2;
result_type draws2;
result_type losses2;
result_type points;
result_type sq_points;
result_type points_lower_bound;
result_type points_upper_bound;
uint64_t n_sims;
};
void fill_skill_table();
Results<uint64_t> play(Field* fd);
void modify_cards(Cards& cards, enum Effect effect);
// Pool-based indexed storage.
//---------------------- Pool-based indexed storage ----------------------------
template<typename T>
class Storage
{
public:
typedef typename std::vector<T*>::size_type size_type;
typedef T value_type;
Storage(size_type size) :
m_pool(sizeof(T))
{
m_indirect.reserve(size);
}
inline T& operator[](size_type i)
{
return(*m_indirect[i]);
}
inline T& add_back()
{
m_indirect.emplace_back((T*) m_pool.malloc());
return(*m_indirect.back());
}
template<typename Pred>
void remove(Pred p)
{
size_type head(0);
for(size_type current(0); current < m_indirect.size(); ++current)
{
if(p((*this)[current]))
{
m_pool.free(m_indirect[current]);
}
else
{
if(current != head)
{
m_indirect[head] = m_indirect[current];
}
++head;
}
}
m_indirect.erase(m_indirect.begin() + head, m_indirect.end());
}
void reset()
{
for(auto index: m_indirect)
{
m_pool.free(index);
}
m_indirect.clear();
}
inline size_type size() const
{
return(m_indirect.size());
}
std::vector<T*> m_indirect;
boost::pool<> m_pool;
};
//------------------------------------------------------------------------------
enum class CardStep
{
none,
attacking,
attacked,
};
//------------------------------------------------------------------------------
struct CardStatus
{
const Card* m_card;
unsigned m_index;
unsigned m_player;
unsigned m_delay;
Faction m_faction;
unsigned m_hp;
CardStep m_step;
unsigned m_berserk;
unsigned m_corroded_rate;
unsigned m_corroded_weakened;
unsigned m_evaded;
unsigned m_enfeebled;
unsigned m_inhibited;
bool m_jammed;
bool m_overloaded;
unsigned m_paybacked;
unsigned m_poisoned;
unsigned m_protected;
unsigned m_rallied;
unsigned m_weakened;
unsigned m_enhanced_value[num_skills];
unsigned m_skill_cd[num_skills];
CardStatus() {}
void set(const Card* card);
void set(const Card& card);
std::string description() const;
bool has_skill(Skill skill_id) const;
template<Skill skill_id> bool has_skill() const;
template<Skill skill_id> unsigned skill() const;
unsigned enhanced(Skill skill) const;
unsigned protected_value() const;
};
//------------------------------------------------------------------------------
// Represents a particular draw from a deck.
// Persistent object: call reset to get a new draw.
class Hand
{
public:
Hand(Deck* deck_) :
deck(deck_),
assaults(15),
structures(15)
{
}
void reset(std::mt19937& re);
Deck* deck;
CardStatus commander;
Storage<CardStatus> assaults;
Storage<CardStatus> structures;
unsigned available_summons;
};
//------------------------------------------------------------------------------
// struct Field is the data model of a battle:
// an attacker and a defender deck, list of assaults and structures, etc.
class Field
{
public:
bool end;
std::mt19937& re;
const Cards& cards;
// players[0]: the attacker, players[1]: the defender
std::array<Hand*, 2> players;
unsigned tapi; // current turn's active player index
unsigned tipi; // and inactive
Hand* tap;
Hand* tip;
std::vector<CardStatus*> selection_array;
unsigned turn;
gamemode_t gamemode;
OptimizationMode optimization_mode;
const Effect effect;
SkillSpec bg_skill;
// With the introduction of on death skills, a single skill can trigger arbitrary many skills.
// They are stored in this, and cleared after all have been performed.
std::deque<std::tuple<CardStatus*, SkillSpec>> skill_queue;
std::vector<CardStatus*> killed_with_on_death;
unsigned n_player_kills;
bool assault_bloodlusted;
unsigned bloodlust_value;
enum phase
{
playcard_phase,
legion_phase,
commander_phase,
structures_phase,
assaults_phase,
end_phase,
};
// the current phase of the turn: starts with playcard_phase, then commander_phase, structures_phase, and assaults_phase
phase current_phase;
// the index of the card being evaluated in the current phase.
// Meaningless in playcard_phase,
// otherwise is the index of the current card in players->structures or players->assaults
unsigned current_ci;
Field(std::mt19937& re_, const Cards& cards_, Hand& hand1, Hand& hand2, gamemode_t gamemode_, OptimizationMode optimization_mode_,
Effect effect_, SkillSpec bg_skill_) :
end{false},
re(re_),
cards(cards_),
players{{&hand1, &hand2}},
turn(1),
gamemode(gamemode_),
optimization_mode(optimization_mode_),
effect(effect_),
bg_skill(bg_skill_),
n_player_kills(0),
assault_bloodlusted(false),
bloodlust_value(0)
{
}
inline unsigned rand(unsigned x, unsigned y)
{
return(std::uniform_int_distribution<unsigned>(x, y)(re));
}
inline unsigned flip()
{
return(this->rand(0,1));
}
template <typename T>
inline T random_in_vector(const std::vector<T>& v)
{
assert(v.size() > 0);
return(v[this->rand(0, v.size() - 1)]);
}
template <typename CardsIter, typename Functor>
inline unsigned make_selection_array(CardsIter first, CardsIter last, Functor f);
inline void print_selection_array();
};
#endif