-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameState.cpp
241 lines (187 loc) · 4.84 KB
/
GameState.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
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
#include "GameState.h"
#include "Player.h"
#include "Cell.h"
#include "Treasure.h"
#include "config.h"
#include <arpa/inet.h>
#include <random>
#include <chrono>
GameState::GameState(int N, int M) : N(N), M(M), T(0), P(0), treasures(M) {
grid = new Cell**[N];
for (int i = 0; i < N; ++i) {
grid[i] = new Cell*[N];
for (int j = 0; j < N; ++j) {
grid[i][j] = NULL;
}
}
}
GameState::~GameState() {
std::lock_guard<std::mutex> lck(state_mutex);
for (const Treasure* treasure: treasures)
delete treasure;
treasures.clear();
for (const auto& pair: players)
delete pair.second;
players.clear();
for (int i = 0; i < N; ++i)
delete[] grid[i];
delete[] grid;
}
void GameState::initState(const char* state, size_t size) {
std::lock_guard<std::mutex> lck(state_mutex);
if (size < 8)
return;
const int* data = (int*) state;
const int* max_data;
T = ntohl(*data);
M = ntohl(*data);
P = ntohl(*(data + 1));
const size_t exp_size = 8 * T + 16 * P + 8;
if (size < exp_size || (T+P) > N*N)
return;
data += 2;
max_data = data + 2 * T;
for ( ; data < max_data; data += 2) {
int x = ntohl(*data);
int y = ntohl(*(data + 1));
Treasure *t = new Treasure(x, y);
treasures.insert(t);
grid[x][y] = t;
}
data = max_data;
max_data = data + 4 * P;
for ( ; data < max_data; data += 4) {
int p_id = ntohl(*data);
int p_T = ntohl(*(data + 1));
int x = ntohl(*(data + 2));
int y = ntohl(*(data + 3));
players[p_id] = new Player(x, y, p_id, p_T);
grid[x][y] = players[p_id];
}
}
void GameState::initTreasures() {
std::lock_guard<std::mutex> lck(state_mutex);
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::minstd_rand generator(seed);
std::uniform_int_distribution<int> distribution(0, N-1);
if (M > N * N)
throw std::string("Cannot create treasures");
while (T < M) {
int x = distribution(generator);
int y = distribution(generator);
if (!grid[x][y]) {
Treasure* treasure = new Treasure(x, y);
grid[x][y] = treasure;
treasures.insert(treasure);
++T;
}
}
}
void GameState::move(int id, char dir) {
std::unique_lock<std::mutex> lck(state_mutex);
auto it = players.find(id);
if (it == players.end())
return;
Player *player = it->second;
int new_x(player->x), new_y(player->y);
switch(dir) {
case 'S':
new_y++;
break;
case 'E':
new_x++;
break;
case 'N':
new_y--;
break;
case 'W':
new_x--;
break;
default:
return;
}
if (checkBounds(new_x, new_y)) {
Cell* cell = grid[new_x][new_y];
if (!cell || cell->isTreasure()) {
if (cell && cell->isTreasure()) {
player->T++;
T--;
treasures.erase(dynamic_cast<Treasure*>(cell));
delete cell;
}
grid[player->x][player->y] = NULL;
grid[new_x][new_y] = player;
player->x = new_x;
player->y = new_y;
}
}
}
bool GameState::checkBounds(int x, int y) const {
return x >= 0 && y >= 0 && x < N && y < N;
}
bool GameState::addPlayer(int id) {
std::lock_guard<std::mutex> lck(state_mutex);
auto it = players.find(id);
if (T + P >= N * N || it != players.end())
return false;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::minstd_rand generator(seed);
std::uniform_int_distribution<int> distribution(0, N-1);
while (true) {
int x = distribution(generator);
int y = distribution(generator);
if (!grid[x][y]) {
Player* player = new Player(x, y, id);
grid[x][y] = player;
players[id] = player;
++P;
return true;
}
}
}
void GameState::removePlayer(int id) {
std::lock_guard<std::mutex> lck(state_mutex);
auto it = players.find(id);
if (it != players.end()) {
Player *player = it->second;
int x(player->x);
int y(player->y);
if (grid[x][y] == player)
grid[x][y] = NULL;
players.erase(it);
delete player;
--P;
}
}
std::string GameState::getState() {
std::lock_guard<std::mutex> lck(state_mutex);
std::string state;
uint32_t nT = htonl(T);
uint32_t nP = htonl(P);
state.append((char*) &nT, 4);
state.append((char*) &nP, 4);
for (Treasure* treasure: treasures) {
uint32_t nx = htonl(treasure->x);
uint32_t ny = htonl(treasure->y);
state.append((char*) &nx, 4);
state.append((char*) &ny, 4);
}
for (const auto& pair: players) {
Player *p = pair.second;
uint32_t nId = htonl(p->id);
uint32_t nT = htonl(p->T);
uint32_t nx = htonl(p->x);
uint32_t ny = htonl(p->y);
state.append((char*) &nId, 4);
state.append((char*) &nT, 4);
state.append((char*) &nx, 4);
state.append((char*) &ny, 4);
}
return state;
}
int GameState::getSize() const {
return N;
}
int GameState::getNbPlayers() const {
return P;
}