-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
173 lines (159 loc) · 5.73 KB
/
main.cpp
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
#include "../nlohmann/json.hpp"
#include <iostream>
#include <fstream>
#include "Constants.h"
#include "Context.h"
#include "Solver.h"
#include "SimpleLog.h"
#include <chrono>
#ifdef REWIND_VIEWER
#include "../rewind/RewindClient.h"
#endif
#include "ExtContext.h"
#include "models/simulation/FoodDetection.h"
using json = nlohmann::json;
int main(int argc, char *argv[]) {
srand(270718);
#ifdef LOCAL_RUN
std::fstream file;
if (argc == 1) {
file.open("data.json", std::ios::out);
} else {
file.open(argv[1], std::ios::in);
}
std::vector<std::pair<ExtContext, std::string>> contexts;
#endif
{
std::string params;
#ifdef LOCAL_RUN
if (argc == 1) {
std::cin >> params;
file << params << std::endl;
} else {
file >> params;
}
#else
std::cin >> params;
#endif
auto parsed = json::parse(params);
// LOG_DEBUG("%", parsed.dump());
Constants::initConstants(parsed);
}
std::string data;
ExtContext context(0);
for (auto &player : context.players) {
player.aimX = NAN;
}
Solver solver;
int micros = 0;
int totalMicros = 0;
int totalTlPrevention = 0;
while (context.getTickIndex() < Constants::GAME_TICKS()) {
LOG_DEBUG("%", context.getTickIndex())
#ifdef LOCAL_RUN
if (argc == 1) {
std::cin >> data;
file << data << std::endl;
} else {
file >> data;
if (file.eof()) {
break;
}
}
LOG_INFO("%", data);
contexts.emplace_back(context, data);
#else
std::cin >> data;
#endif
using std::chrono::system_clock;
system_clock::time_point start = system_clock::now();
auto parsed = json::parse(data);
auto command = solver.onTick(context, parsed);
#ifdef REWIND_VIEWER
RewindClient &instance = RewindClient::instance();
double cX = command.getX();
double cY = command.getY();
for (auto &player : context.me->parts) {
instance.line(player.getX(), player.getY(), cX, cY, 0xFF8800, 3);
}
instance.line(cX + 5, cY + 5, cX - 5, cY - 5, 0xFF8800, 3);
instance.line(cX - 5, cY + 5, cX + 5, cY - 5, 0xFF8800, 3);
#ifdef DRAW_PP
// draw food field
for (int i = 0; i != Constants::EAT_FIELD_COUNT; ++i) {
for (int j = 0; j != Constants::EAT_FIELD_COUNT; ++j) {
double color = std::min(FoodDetection::foodField[i][j] * 20., 1.);
auto green = (uint32_t) (255 * color + .01);
green <<= 8; // green channel
instance.rect(i * Constants::COORDS_IN_ONE_FOOD_FIELD,
j * Constants::COORDS_IN_ONE_FOOD_FIELD,
(i + 1) * Constants::COORDS_IN_ONE_FOOD_FIELD,
(j + 1) * Constants::COORDS_IN_ONE_FOOD_FIELD,
green,
1);
}
}
#endif
instance.end_frame();
#endif
system_clock::time_point end = system_clock::now();
command.appendDebug(" time: " + std::to_string(micros));
if (context.tlPrevention > 0) {
totalTlPrevention += context.tlPrevention;
command.appendDebug(" tlPrevention: " + std::to_string(context.tlPrevention) + " totalTlPrevention: " + std::to_string(totalTlPrevention));
} else if (context.disableAddPoints) {
command.appendDebug(" add points disabled");
}
if (context.me->parts.empty() || context.getTickIndex() + 1 == Constants::GAME_TICKS()) {
totalMicros += micros;
micros = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
totalMicros += micros;
command.appendDebug(" dead. avg time: " + std::to_string(((double) totalMicros) / (context.getTickIndex() + 1)));
if (context.tlPrevention <= 0) {
command.appendDebug(" totalTlPrevention: " + std::to_string(totalTlPrevention));
}
std::cout << command.toJson().dump() << std::endl;
break;
}
std::cout << command.toJson().dump() << std::endl;
#ifdef PERF
std::cout << context.getTickIndex() << std::endl;
#endif
micros = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
totalMicros += micros;
context.totalLength = totalMicros;
context.turnTime.push_back(micros);
LOG_DEBUG("%", command.toJson().dump())
context.nextTick(command.getX(), command.getY());
}
#ifdef LOCAL_RUN
#ifndef PERF
file.close();
std::cout << Command(0, 0, "dead. avg time: " + std::to_string(((double) totalMicros) / (context.getTickIndex() + 1))).toJson().dump() << std::endl;
int idxToDebug;
while (true) {
std::cout << "type index of debug tick (exit if negative)" << std::endl;
std::cin >> idxToDebug;
if (idxToDebug < 0) {
break;
}
ExtContext contextCopy = contexts[idxToDebug].first;
auto dataCopy = json::parse(contexts[idxToDebug].second);
auto command = solver.onTick(contextCopy, dataCopy);
std::cout << command.toJson().dump() << std::endl;
#ifdef REWIND_VIEWER
RewindClient &instance = RewindClient::instance();
double cX = command.getX();
double cY = command.getY();
for (auto &player : contextCopy.me->parts) {
instance.line(player.getX(), player.getY(), cX, cY, 0xFF8800, 3);
}
instance.line(cX + 5, cY + 5, cX - 5, cY - 5, 0xFF8800, 3);
instance.line(cX - 5, cY + 5, cX + 5, cY - 5, 0xFF8800, 3);
instance.end_frame();
#endif
}
#endif
#endif
return 0;
}