-
Notifications
You must be signed in to change notification settings - Fork 14
/
sim.h
330 lines (301 loc) · 8.47 KB
/
sim.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#ifndef SIM_H_INCLUDED
#define SIM_H_INCLUDED
#include <boost/pool/pool.hpp>
#include <string>
#include <array>
#include <deque>
#include <tuple>
#include <vector>
#include <random>
#include "tyrant.h"
class Card;
class Cards;
class Deck;
class Field;
class Achievement;
extern unsigned debug_print;
extern unsigned debug_cached;
extern bool debug_line;
extern std::string debug_str;
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;
}
};
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_augmented;
unsigned m_berserk;
bool m_blitzing;
bool m_chaosed;
unsigned m_corroded;
unsigned m_corrosion_speed;
unsigned m_delay;
bool m_diseased;
unsigned m_enfeebled;
unsigned m_enhance_armored;
unsigned m_enhance_berserk;
unsigned m_enhance_corrosive;
unsigned m_enhance_counter;
unsigned m_enhance_enfeeble;
unsigned m_enhance_evade;
unsigned m_enhance_heal;
unsigned m_enhance_leech;
unsigned m_enhance_poison;
unsigned m_enhance_rally;
unsigned m_enhance_strike;
unsigned m_evades_left;
Faction m_faction;
unsigned m_flurry_charge;
bool m_frozen;
bool m_has_jammed;
unsigned m_hp;
bool m_immobilized;
bool m_infused;
unsigned m_inhibited;
bool m_jammed;
unsigned m_jam_charge;
bool m_phased;
unsigned m_poisoned;
unsigned m_protected;
unsigned m_rallied;
unsigned m_stunned;
bool m_sundered;
unsigned m_weakened;
bool m_temporary_split;
bool m_is_summoned; // is this card summoned (or split)?
CardStep m_step;
CardStatus() {}
CardStatus(const Card* card);
void set(const Card* card);
void set(const Card& card);
std::string description();
};
//------------------------------------------------------------------------------
// 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;
CardStatus fortress1;
CardStatus fortress2;
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;
const Achievement& achievement;
// 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;
std::vector<CardStatus*> killed_with_regen;
enum phase
{
playcard_phase,
legion_phase,
commander_phase,
structures_phase,
assaults_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;
unsigned last_decision_turn;
// unsigned points_since_last_decision;
unsigned fusion_count;
std::vector<unsigned> achievement_counter;
Field(std::mt19937& re_, const Cards& cards_, Hand& hand1, Hand& hand2, gamemode_t gamemode_, OptimizationMode optimization_mode_, Effect effect_, const Achievement& achievement_) :
end{false},
re(re_),
cards(cards_),
players{{&hand1, &hand2}},
turn(1),
gamemode(gamemode_),
optimization_mode(optimization_mode_),
effect(effect_),
achievement(achievement_)
{
}
inline unsigned rand(unsigned x, unsigned y)
{
return(std::uniform_int_distribution<unsigned>(x, y)(re));
}
inline unsigned flip()
{
std::cout << "WARNING!!! Field-flip() was called. THIS SHOULD NOT HAPPEN FOR TYRANT UNLEASHED\n";
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();
template <class T>
inline void set_counter(T& container, unsigned key, unsigned value)
{
auto x = container.find(key);
if(x != container.end())
{
achievement_counter[x->second] = value;
}
}
template <class T>
inline void inc_counter(T& container, unsigned key, unsigned value = 1)
{
auto x = container.find(key);
if(x != container.end())
{
achievement_counter[x->second] += value;
#if 0
if(achievement.req_counter[x->second].predict_monoinc(achievement_counter[x->second]) < 0)
{
end = true;
}
#endif
}
}
template <class T>
inline void update_max_counter(T& container, unsigned key, unsigned value)
{
auto x = container.find(key);
if(x != container.end() && achievement_counter[x->second] < value)
{
achievement_counter[x->second] = value;
#if 0
if(achievement.req_counter[x->second].predict_monoinc(achievement_counter[x->second]) < 0)
{
end = true;
}
#endif
}
}
};
#endif