-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simulator.hpp
347 lines (283 loc) · 6.64 KB
/
Simulator.hpp
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifndef RAIC2019_SIMULATOR_HPP
#define RAIC2019_SIMULATOR_HPP
#include <vector>
#include <cassert>
#include "model/Game.hpp"
#include "myutils.hpp"
#include <random>
#include <map>
struct MyLevel {
std::vector<Tile> tiles;
std::vector<P> spawns;
Properties properties;
uint32_t w, h;
double microtickT;
int unitJumpTimeMicrotics;
int jumpPadJumpTimeMicrotics;
double unitW05;
double unitH05;
inline Tile getTile(int x, int y) const {
assert(x >= 0 && x < (int) w);
assert(y >= 0 && y < (int) h);
return tiles[y * w + x];
}
inline Tile getTile(IP p) const {
return getTile(p.x, p.y);
}
inline Tile getTileSafe(IP p) const {
if (isValid(p))
return getTile(p.x, p.y);
return TILE_NONE;
}
int toMicroticks(double t) const {
return t / microtickT + 0.5;
}
bool isValidX(int x) const {
return x >= 0 && x < (int) w;
}
bool isValidY(int y) const {
return y >= 0 && y < (int) h;
}
bool isValid(IP p) const {
return p.x >= 0 && p.x < (int) w && p.y >= 0 && p.y < (int) h;
}
P rayWallCollision(const P &p, const P &ray) const;
P bboxWallCollision(const BBox &p, const P &ray) const; // В этой версии баг!
P bboxWallCollision2(const BBox &p, const P &ray) const;
};
enum class Side
{
NONE = -1, ME, EN
};
struct MyAction
{
P aim = P(1, 0);
P rawAim = P(0, 0);
double velocity = 0;
bool jump = false;
bool jumpDown = false;
bool shoot = false;
bool reload = false;
bool swapWeapon = false;
bool plantMine = false;
};
enum class MyWeaponType
{
NONE = -1,
PISTOL = 0,
ASSAULT_RIFLE = 1,
ROCKET_LAUNCHER = 2
};
inline std::string getWeaponName(MyWeaponType t)
{
switch (t)
{
case MyWeaponType::NONE:
return "NONE";
case MyWeaponType::PISTOL:
return "PISTOL";
case MyWeaponType::ASSAULT_RIFLE:
return "ASSAULT_RIFLE";
case MyWeaponType::ROCKET_LAUNCHER:
return "ROCKET_LAUNCHER";
}
return "";
}
struct MyWeaponParams {
int magazineSize;
int fireRateMicrotiks;
int reloadTimeMicrotiks;
double minSpread;
double maxSpread;
double recoil;
double aimSpeed;
double bulletSpeed;
double bulletSize;
int bulletDamage;
double explRadius;
int explDamage;
};
extern MyWeaponParams WEAPON_PARAMS[3];
inline MyWeaponParams *getWeaponParams(MyWeaponType weaponType)
{
return &WEAPON_PARAMS[(int) weaponType];
}
struct MyWeapon
{
MyWeaponType weaponType = MyWeaponType::NONE;
MyWeaponParams *params;
int magazine;
bool wasShooting;
double spread;
int fireTimerMicrotics;
P lastAngle;
int lastFireTick = -1;
explicit operator bool() const
{
return weaponType != MyWeaponType ::NONE;
}
};
struct MyUnit {
P pos;
P _oldPos, _vel;
P size;
int maxTimeMicrotics;
int playerId;
int id;
int health;
int mines;
Side side;
MyWeapon weapon;
bool canCancel;
bool canJump;
bool walkedRight;
bool stand;
bool onGround;
bool onLadder;
MyAction action;
BBox getBBox() const {
BBox res;
res.minP = pos - P(size.x * 0.5, 0);
res.maxP = pos + P(size.x * 0.5, size.y);
return res;
}
P getCenter() const {
return pos + P(0, size.y * 0.5);
}
bool isMine() const {
return side == Side::ME;
}
bool isEnemy() const {
return side == Side::EN;
}
};
enum class MyLootBoxType {
NONE = -1, HEALTH_PACK, WEAPON, MINE
};
struct MyLootBox {
P pos, size;
MyLootBoxType type;
int health;
MyWeaponType weaponType;
BBox getBBox() const {
BBox res;
res.minP = pos - P(size.x * 0.5, 0);
res.maxP = pos + P(size.x * 0.5, size.y);
return res;
}
};
struct MyBullet {
int unitId;
Side side;
MyWeaponType weaponType;
P pos, vel;
double size05;
MyWeaponParams *params;
BBox getBBox() const {
BBox res;
res.minP = pos - P(size05, size05);
res.maxP = pos + P(size05, size05);
return res;
}
BBox getExplosionBBox() const {
BBox res;
res.minP = pos - P(params->explRadius, params->explRadius);
res.maxP = pos + P(params->explRadius, params->explRadius);
return res;
}
};
struct MyMine {
P pos, size;
double triggerRadius;
int timerMicroticks;
Side side;
MineState state;
BBox getBBox() const {
BBox res;
res.minP = pos - P(size.x * 0.5, 0);
res.maxP = pos + P(size.x * 0.5, size.y);
return res;
}
P getCenter() const {
return pos + P(0, size.y * 0.5);
}
};
inline std::string getMineStateName(MineState state)
{
switch (state)
{
case PREPARING:
return "Preparing";
case IDLE:
return "Idle";
case TRIGGERED:
return "Triggered";
case EXPLODED:
return "Exploded";
}
return "";
}
struct Simulator {
std::minstd_rand rnd;
std::uniform_real_distribution<double> dis = std::uniform_real_distribution<double>(-1.0, 1.0);
bool enFireAtCenter = false;
int myRLFireDir = -1;
bool log = false;
double getRandom()
{
return dis(rnd);
}
const MyLevel *level;
int curTick = -1;
std::vector<MyUnit> units;
std::vector<MyBullet> bullets;
std::vector<MyMine> mines;
std::vector<MyLootBox> lootBoxes;
MyUnit *getUnit(int id) {
for (MyUnit &m : units)
{
if (m.id == id)
return &m;
}
return nullptr;
}
int score[2] = {};
double selfFire[2] = {};
std::map<int, int> friendlyFireByUnitId;
void tickNoOpt();
void tick();
void microtick(int subtickInd, int microtickCount);
};
inline bool wallOrPlatformOrLadder(Tile tile)
{
return tile == WALL || tile == PLATFORM || tile == LADDER;
}
inline bool wallOrPlatform(Tile tile)
{
return tile == WALL || tile == PLATFORM;
}
inline bool wall(Tile tile)
{
return tile == WALL;
}
inline bool wallOrLadder(Tile tile)
{
return tile == WALL || tile == LADDER;
}
inline bool wallOrPlatformOrLadderOrJumpPad(Tile tile)
{
return tile == WALL || tile == PLATFORM || tile == LADDER || tile == JUMP_PAD;
}
inline bool wallOrJumppad(Tile tile)
{
return tile == WALL || tile == JUMP_PAD;
}
inline bool jumppad(Tile tile)
{
return tile == JUMP_PAD;
}
inline bool emptyOrPlatformOrLadder(Tile tile)
{
return tile == EMPTY || tile == PLATFORM || tile == LADDER;
}
#endif