This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Game.cpp
280 lines (232 loc) · 8.38 KB
/
Game.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
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
#include "Game.hpp"
#include "Connection.hpp"
#include <stdexcept>
#include <iostream>
#include <cstring>
#include <glm/gtx/norm.hpp>
void Player::Controls::send_controls_message(Connection *connection_) const {
assert(connection_);
auto &connection = *connection_;
uint32_t size = 5;
connection.send(Message::C2S_Controls);
connection.send(uint8_t(size));
connection.send(uint8_t(size >> 8));
connection.send(uint8_t(size >> 16));
auto send_button = [&](Button const &b) {
if (b.downs & 0x80) {
std::cerr << "Wow, you are really good at pressing buttons!" << std::endl;
}
connection.send(uint8_t( (b.pressed ? 0x80 : 0x00) | (b.downs & 0x7f) ) );
};
send_button(left);
send_button(right);
send_button(up);
send_button(down);
send_button(jump);
}
bool Player::Controls::recv_controls_message(Connection *connection_) {
assert(connection_);
auto &connection = *connection_;
auto &recv_buffer = connection.recv_buffer;
//expecting [type, size_low0, size_mid8, size_high8]:
if (recv_buffer.size() < 4) return false;
if (recv_buffer[0] != uint8_t(Message::C2S_Controls)) return false;
uint32_t size = (uint32_t(recv_buffer[3]) << 16)
| (uint32_t(recv_buffer[2]) << 8)
| uint32_t(recv_buffer[1]);
if (size != 5) throw std::runtime_error("Controls message with size " + std::to_string(size) + " != 5!");
//expecting complete message:
if (recv_buffer.size() < 4 + size) return false;
auto recv_button = [](uint8_t byte, Button *button) {
button->pressed = (byte & 0x80);
uint32_t d = uint32_t(button->downs) + uint32_t(byte & 0x7f);
if (d > 255) {
std::cerr << "got a whole lot of downs" << std::endl;
d = 255;
}
button->downs = uint8_t(d);
};
recv_button(recv_buffer[4+0], &left);
recv_button(recv_buffer[4+1], &right);
recv_button(recv_buffer[4+2], &up);
recv_button(recv_buffer[4+3], &down);
recv_button(recv_buffer[4+4], &jump);
//delete message from buffer:
recv_buffer.erase(recv_buffer.begin(), recv_buffer.begin() + 4 + size);
return true;
}
//-----------------------------------------
Game::Game() : mt(0x15466666) {
}
Player *Game::spawn_player() {
players.emplace_back();
Player &player = players.back();
//random point in the middle area of the arena:
player.position.x = glm::mix(ArenaMin.x + 2.0f * PlayerRadius, ArenaMax.x - 2.0f * PlayerRadius, 0.4f + 0.2f * mt() / float(mt.max()));
player.position.y = glm::mix(ArenaMin.y + 2.0f * PlayerRadius, ArenaMax.y - 2.0f * PlayerRadius, 0.4f + 0.2f * mt() / float(mt.max()));
do {
player.color.r = mt() / float(mt.max());
player.color.g = mt() / float(mt.max());
player.color.b = mt() / float(mt.max());
} while (player.color == glm::vec3(0.0f));
player.color = glm::normalize(player.color);
player.name = "Player " + std::to_string(next_player_number++);
return &player;
}
void Game::remove_player(Player *player) {
bool found = false;
for (auto pi = players.begin(); pi != players.end(); ++pi) {
if (&*pi == player) {
players.erase(pi);
found = true;
break;
}
}
assert(found);
}
void Game::update(float elapsed) {
//position/velocity update:
for (auto &p : players) {
glm::vec2 dir = glm::vec2(0.0f, 0.0f);
if (p.controls.left.pressed) dir.x -= 1.0f;
if (p.controls.right.pressed) dir.x += 1.0f;
if (p.controls.down.pressed) dir.y -= 1.0f;
if (p.controls.up.pressed) dir.y += 1.0f;
if (dir == glm::vec2(0.0f)) {
//no inputs: just drift to a stop
float amt = 1.0f - std::pow(0.5f, elapsed / (PlayerAccelHalflife * 2.0f));
p.velocity = glm::mix(p.velocity, glm::vec2(0.0f,0.0f), amt);
} else {
//inputs: tween velocity to target direction
dir = glm::normalize(dir);
float amt = 1.0f - std::pow(0.5f, elapsed / PlayerAccelHalflife);
//accelerate along velocity (if not fast enough):
float along = glm::dot(p.velocity, dir);
if (along < PlayerSpeed) {
along = glm::mix(along, PlayerSpeed, amt);
}
//damp perpendicular velocity:
float perp = glm::dot(p.velocity, glm::vec2(-dir.y, dir.x));
perp = glm::mix(perp, 0.0f, amt);
p.velocity = dir * along + glm::vec2(-dir.y, dir.x) * perp;
}
p.position += p.velocity * elapsed;
//reset 'downs' since controls have been handled:
p.controls.left.downs = 0;
p.controls.right.downs = 0;
p.controls.up.downs = 0;
p.controls.down.downs = 0;
p.controls.jump.downs = 0;
}
//collision resolution:
for (auto &p1 : players) {
//player/player collisions:
for (auto &p2 : players) {
if (&p1 == &p2) break;
glm::vec2 p12 = p2.position - p1.position;
float len2 = glm::length2(p12);
if (len2 > (2.0f * PlayerRadius) * (2.0f * PlayerRadius)) continue;
if (len2 == 0.0f) continue;
glm::vec2 dir = p12 / std::sqrt(len2);
//mirror velocity to be in separating direction:
glm::vec2 v12 = p2.velocity - p1.velocity;
glm::vec2 delta_v12 = dir * glm::max(0.0f, -1.75f * glm::dot(dir, v12));
p2.velocity += 0.5f * delta_v12;
p1.velocity -= 0.5f * delta_v12;
}
//player/arena collisions:
if (p1.position.x < ArenaMin.x + PlayerRadius) {
p1.position.x = ArenaMin.x + PlayerRadius;
p1.velocity.x = std::abs(p1.velocity.x);
}
if (p1.position.x > ArenaMax.x - PlayerRadius) {
p1.position.x = ArenaMax.x - PlayerRadius;
p1.velocity.x =-std::abs(p1.velocity.x);
}
if (p1.position.y < ArenaMin.y + PlayerRadius) {
p1.position.y = ArenaMin.y + PlayerRadius;
p1.velocity.y = std::abs(p1.velocity.y);
}
if (p1.position.y > ArenaMax.y - PlayerRadius) {
p1.position.y = ArenaMax.y - PlayerRadius;
p1.velocity.y =-std::abs(p1.velocity.y);
}
}
}
void Game::send_state_message(Connection *connection_, Player *connection_player) const {
assert(connection_);
auto &connection = *connection_;
connection.send(Message::S2C_State);
//will patch message size in later, for now placeholder bytes:
connection.send(uint8_t(0));
connection.send(uint8_t(0));
connection.send(uint8_t(0));
size_t mark = connection.send_buffer.size(); //keep track of this position in the buffer
//send player info helper:
auto send_player = [&](Player const &player) {
connection.send(player.position);
connection.send(player.velocity);
connection.send(player.color);
//NOTE: can't just 'send(name)' because player.name is not plain-old-data type.
//effectively: truncates player name to 255 chars
uint8_t len = uint8_t(std::min< size_t >(255, player.name.size()));
connection.send(len);
connection.send_buffer.insert(connection.send_buffer.end(), player.name.begin(), player.name.begin() + len);
};
//player count:
connection.send(uint8_t(players.size()));
if (connection_player) send_player(*connection_player);
for (auto const &player : players) {
if (&player == connection_player) continue;
send_player(player);
}
//compute the message size and patch into the message header:
uint32_t size = uint32_t(connection.send_buffer.size() - mark);
connection.send_buffer[mark-3] = uint8_t(size);
connection.send_buffer[mark-2] = uint8_t(size >> 8);
connection.send_buffer[mark-1] = uint8_t(size >> 16);
}
bool Game::recv_state_message(Connection *connection_) {
assert(connection_);
auto &connection = *connection_;
auto &recv_buffer = connection.recv_buffer;
if (recv_buffer.size() < 4) return false;
if (recv_buffer[0] != uint8_t(Message::S2C_State)) return false;
uint32_t size = (uint32_t(recv_buffer[3]) << 16)
| (uint32_t(recv_buffer[2]) << 8)
| uint32_t(recv_buffer[1]);
uint32_t at = 0;
//expecting complete message:
if (recv_buffer.size() < 4 + size) return false;
//copy bytes from buffer and advance position:
auto read = [&](auto *val) {
if (at + sizeof(*val) > size) {
throw std::runtime_error("Ran out of bytes reading state message.");
}
std::memcpy(val, &recv_buffer[4 + at], sizeof(*val));
at += sizeof(*val);
};
players.clear();
uint8_t player_count;
read(&player_count);
for (uint8_t i = 0; i < player_count; ++i) {
players.emplace_back();
Player &player = players.back();
read(&player.position);
read(&player.velocity);
read(&player.color);
uint8_t name_len;
read(&name_len);
//n.b. would probably be more efficient to directly copy from recv_buffer, but I think this is clearer:
player.name = "";
for (uint8_t n = 0; n < name_len; ++n) {
char c;
read(&c);
player.name += c;
}
}
if (at != size) throw std::runtime_error("Trailing data in state message.");
//delete message from buffer:
recv_buffer.erase(recv_buffer.begin(), recv_buffer.begin() + 4 + size);
return true;
}