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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
#include "client.hh"
namespace {
bool should_send_move = false; // Ratelimit move packets by player packets.
shared::world::block last_block = shared::world::block::type::dirt;
client::world::chunk::map chunks{4096, shared::world::chunk::hash,
shared::world::chunk::equal};
client::players players;
} // namespace
namespace client {
static auto get_player_it(const std::uint32_t index) {
return std::ranges::find_if(
::players, [&](const auto& player) { return player.index == index; });
}
static proto::packet
make_get_chunk_packet(const shared::math::coords& coords) noexcept {
proto::packet packet;
const auto chunk_packet = packet.mutable_request_chunk_packet();
shared::net::set_coords(*chunk_packet->mutable_chunk_pos(), coords);
return packet;
}
static proto::packet make_auth_packet(const std::string& username,
const std::string& password) noexcept {
proto::packet packet;
const auto auth_packet = packet.mutable_auth_packet();
auth_packet->set_username(username);
auth_packet->set_password(password);
return packet;
}
static proto::packet make_add_block_packet(const shared::math::coords& coords,
const glm::ivec3& pos) noexcept {
proto::packet packet;
const auto interact_packet = packet.mutable_add_block_packet();
shared::net::set_coords(*interact_packet->mutable_chunk_pos(), coords);
shared::net::set_ivec3(*interact_packet->mutable_block_pos(), pos);
interact_packet->set_block(static_cast<std::uint32_t>(::last_block.type));
return packet;
}
static proto::packet
make_remove_block_packet(const shared::math::coords& coords,
const glm::ivec3& pos) noexcept {
proto::packet packet;
const auto interact_packet = packet.mutable_remove_block_packet();
shared::net::set_coords(*interact_packet->mutable_chunk_pos(), coords);
shared::net::set_ivec3(*interact_packet->mutable_block_pos(), pos);
return packet;
}
[[maybe_unused]] // [[actually_used]]
static proto::packet
make_remove_chunk_packet(const shared::math::coords& coords) noexcept {
proto::packet packet;
const auto remove_chunk_packet = packet.mutable_remove_chunk_packet();
shared::net::set_coords(*remove_chunk_packet->mutable_chunk_pos(), coords);
return packet;
}
static void handle_init_packet(const proto::init& packet) noexcept {
client::state.seed = packet.seed();
client::state.draw_distance =
std::min(packet.draw_distance(),
client::settings::get({"video", "draw_distance"}, 32));
auto& localplayer = packet.localplayer();
client::state.localplayer = localplayer.index();
::players.emplace_back(shared::net::get_player(localplayer));
}
static void handle_player_packet(const proto::player& packet) noexcept {
if (std::size(::players) <= 0) {
return;
}
const auto player_it = get_player_it(packet.index());
shared::player player = shared::net::get_player(packet);
if (player_it == std::end(::players)) {
::players.emplace_back(player);
return;
}
// If the player packet refers to us, we do not override our localplayer's
// commands or viewangles. Also, we should send a new move packet.
if (auto& lp = get_localplayer(::players); lp.index == packet.index()) {
player.viewangles = lp.viewangles;
player.commands = 0u;
::should_send_move = true;
}
player_it->update(player);
}
// Remove the client whose element is equal to pkt.index.
static void handle_remove_packet(const proto::remove_player& packet) noexcept {
const auto player_it = get_player_it(packet.index());
if (player_it == std::end(::players)) {
return;
}
::players.erase(player_it);
}
static void handle_hear_packet(const proto::hear_player& packet) noexcept {
const auto player_it = get_player_it(packet.index());
if (player_it == std::end(::players)) {
return;
}
player_it->message.emplace(packet.text());
}
static void handle_chunk_packet(const proto::chunk& packet) noexcept {
const shared::math::coords pos{.x = packet.chunk_pos().x(),
.z = packet.chunk_pos().z()};
const auto find_it = ::chunks.find(pos);
if (find_it == std::end(::chunks)) {
return;
}
find_it->second.emplace(
client::state.seed, pos,
shared::world::chunk::make_blocks_from_chunk(packet));
// Force the surrounding chunks to regenerate their vbos.
// It's possible we could only generate the ones that we actually need to
// generate, but that's not really a priority atm.
for (auto x = -1; x <= 1; ++x) {
for (auto z = -1; z <= 1; ++z) {
if (std::abs(x) == 1 && std::abs(z) == 1) {
continue;
}
const auto find_update_it =
::chunks.find(pos + shared::math::coords{x, z});
if (find_update_it == std::end(::chunks)) {
continue;
}
if (!find_update_it->second.has_value()) {
continue;
}
find_update_it->second->should_regenerate_vbo = true;
}
}
}
static void
handle_server_message_packet(const proto::server_message& packet) noexcept {
const bool fatal = packet.fatal();
const std::string message =
"client: received " + std::string{fatal ? "fatal" : ""} +
" message from the server \"" + packet.message() + "\"\n";
if (!fatal) {
shared::print::notify(message);
return;
}
shared::print::warn(message);
shared::should_exit = true;
}
static void parse_packet(const proto::packet& packet) noexcept {
if (packet.has_init_packet()) {
handle_init_packet(packet.init_packet());
} else if (packet.has_player_packet()) {
handle_player_packet(packet.player_packet());
} else if (packet.has_remove_player_packet()) {
handle_remove_packet(packet.remove_player_packet());
} else if (packet.has_hear_player_packet()) {
handle_hear_packet(packet.hear_player_packet());
} else if (packet.has_chunk_packet()) {
handle_chunk_packet(packet.chunk_packet());
} else if (packet.has_server_message_packet()) {
handle_server_message_packet(packet.server_message_packet());
}
#ifndef NDEBUG
else {
shared::print::warn("client: unhandled packet type\n");
}
#endif
}
static shared::net::connection make_connection(const std::string_view address,
const std::string_view port) {
constexpr addrinfo hints = {.ai_flags = AI_PASSIVE,
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM};
const auto info = shared::net::get_addr_info(address, port, &hints);
const auto sock = shared::net::make_socket(info.get());
shared::net::connect_socket(sock, info.get());
return shared::net::connection(sock);
}
static void update_chunks(shared::net::connection& connection) noexcept {
const auto draw_distance = client::state.draw_distance;
const shared::math::coords& lp_pos = get_localplayer(::players).chunk_pos;
// Remove bad chunks.
std::erase_if(::chunks, [&](const auto& chunk) {
const bool should_erase =
!shared::math::is_inside_draw(chunk.first, lp_pos, draw_distance);
if (should_erase) {
connection.rsend_packet(make_remove_chunk_packet(chunk.first));
}
return should_erase;
});
for (int dist = 0; dist <= client::state.draw_distance; ++dist) {
const auto maybe_add_chunk = [&](const int x, const int z) -> bool {
const auto pos = shared::math::coords{x + lp_pos.x, z + lp_pos.z};
if (!is_inside_draw(pos, lp_pos, draw_distance)) {
return false;
}
if (::chunks.contains(pos)) {
return false;
}
connection.rsend_packet(make_get_chunk_packet(pos));
::chunks.emplace(pos, std::nullopt);
return true;
};
int x = -dist;
int z = dist;
// Does a spiral pattern, but it's basically unnoticable :(
for (int i = 0; i < dist * 2 + 1; ++i) {
if (maybe_add_chunk(x, z)) {
return;
}
x += (i < dist * 2);
}
for (int i = 0; i < dist * 2; ++i) {
--z;
if (maybe_add_chunk(x, z)) {
return;
}
}
for (int i = 0; i < dist * 2; ++i) {
--x;
if (maybe_add_chunk(x, z)) {
return;
}
}
for (int i = 0; i < dist * 2 - 1; ++i) {
++z;
if (maybe_add_chunk(x, z)) {
return;
}
}
}
}
static void update_state() noexcept {
client::state.player_count = ::players.size();
client::state.requested_chunk_count =
static_cast<std::uint32_t>(std::ranges::count_if(
::chunks, [](const auto& c) { return !c.second.has_value(); }));
client::state.networked_chunk_count =
std::size(::chunks) - client::state.requested_chunk_count;
}
static proto::packet make_say_packet(const std::string& text) noexcept {
proto::packet packet;
const auto sub_say_packet = packet.mutable_say_packet();
sub_say_packet->set_text(text);
return packet;
}
// unfortunate non-static :(
void send_say_packet(const std::string& text) noexcept {
client::state.connection->rsend_packet(make_say_packet(text));
}
static void handle_button_input() noexcept {
if (input::is_key_pressed(SDLK_z)) {
client::render::camera::get_xfov() = 30.0f;
} else {
client::render::camera::get_xfov() =
client::settings::get({"gameplay", "fov"}, 100.0f);
}
// Don't build our movement commands if we're inputting text.
if (input::state.typing) {
return;
}
auto& lp = get_localplayer(::players);
using spm = shared::player::mask;
lp.commands |= spm::forward * input::is_key_pressed(SDLK_w);
lp.commands |= spm::left * input::is_key_pressed(SDLK_a);
lp.commands |= spm::backward * input::is_key_pressed(SDLK_s);
lp.commands |= spm::right * input::is_key_pressed(SDLK_d);
lp.commands |= spm::jump * input::is_key_pressed(SDLK_SPACE);
lp.commands |= spm::crouch * input::is_key_pressed(SDLK_LCTRL);
lp.commands |= spm::sprint * input::is_key_pressed(SDLK_LSHIFT);
lp.commands |= spm::attack * input::is_key_pressed(SDL_BUTTON_LEFT);
}
static void update_input() noexcept {
client::input::update();
if (!client::window::is_open()) {
handle_button_input();
}
if (client::input::state.quit) {
shared::should_exit = true;
}
}
static proto::packet
make_move_packet(const shared::player& localplayer) noexcept {
proto::packet packet;
const auto move_packet = packet.mutable_move_packet();
move_packet->set_commands(localplayer.commands);
shared::net::set_angles(*move_packet->mutable_viewangles(),
localplayer.viewangles);
return packet;
}
static void send_move_packet(shared::net::connection& connection) noexcept {
const auto& localplayer = get_localplayer(::players);
connection.usend_packet(make_move_packet(localplayer));
}
static void update_players() noexcept {
const auto lp_index = get_localplayer(::players).index;
// pvs, remove clients outside of chunks we own
std::erase_if(::players, [&](const auto& player) {
if (player.index == lp_index) {
return false;
}
// Players should be removed if the chunk can't draw.
const auto chunk_it = ::chunks.find(player.chunk_pos);
if (chunk_it == std::end(::chunks)) {
return true;
}
const auto& chunk = chunk_it->second;
if (!chunk.has_value()) {
return false;
}
if (!chunk->can_draw()) {
return true;
}
return false;
});
}
// requires SDL_MOUSEMOTION
static void handle_mousemotion(const SDL_Event& event) noexcept {
const float sens =
settings::get<float>({"gameplay", "mouse_sensitivity"}, 0.0235f);
auto& lp = get_localplayer(::players);
auto& angles = lp.viewangles;
const float pitch_offset = static_cast<float>(event.motion.yrel) * sens;
const float yaw_offset = static_cast<float>(event.motion.xrel) * sens;
angles.pitch = std::clamp(angles.pitch - glm::radians(pitch_offset),
glm::radians(-89.0f), glm::radians(89.0f));
angles.yaw =
std::fmod(angles.yaw + glm::radians(yaw_offset), glm::radians(360.0f)) +
(angles.yaw < 0.0f) * glm::radians(360.0f);
}
// requires SDL_MOUSEBUTTONDOWN
static void handle_mousebuttons(const SDL_Event& event) noexcept {
if (event.button.button != SDL_BUTTON_LEFT &&
event.button.button != SDL_BUTTON_RIGHT) {
return;
}
const auto mode = event.button.button == SDL_BUTTON_LEFT
? client::movement::interact_mode::remove
: client::movement::interact_mode::add;
const auto position =
client::movement::interact(get_localplayer(::players), mode, ::chunks);
if (!position.has_value()) {
return;
}
const auto& [chunk_pos, block_pos] = *position;
auto& connection = *client::state.connection;
switch (mode) {
case client::movement::interact_mode::add:
connection.usend_packet(make_add_block_packet(chunk_pos, block_pos));
break;
case client::movement::interact_mode::remove:
connection.usend_packet(make_remove_block_packet(chunk_pos, block_pos));
::last_block = ::chunks[chunk_pos]->get_block(block_pos);
break;
}
}
static void handle_events(const SDL_Event& event) noexcept {
if (client::window::is_open()) {
return;
}
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
handle_mousebuttons(event);
break;
case SDL_MOUSEMOTION:
handle_mousemotion(event);
break;
default:
break;
}
}
static void authenticate_client(shared::net::connection& connection) noexcept {
const auto rand_alphanum_string = [](const int size) -> std::string {
static const std::string allowed = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"1234567890";
std::string ret;
for (int i = 0; i < size; ++i) {
std::ranges::sample(allowed, std::back_inserter(ret), 1,
std::random_device{});
}
return ret;
};
const std::string username = settings::get<std::string>(
{"auth", "username"},
rand_alphanum_string(shared::MAX_USER_PASS_LENGTH));
const std::string password = settings::get<std::string>(
{"auth", "password"},
rand_alphanum_string(shared::MAX_USER_PASS_LENGTH));
connection.rsend_packet(make_auth_packet(username, password));
}
void main(const std::string_view address, const std::string_view port) {
client::state.address = address;
client::state.port = port;
shared::net::connection connection = make_connection(address, port);
shared::print::notify("client: connected to " + std::string{address} + ':' +
std::string{port} + '\n');
client::state.connection = &connection;
client::render::init();
client::input::register_event_handler(&handle_events);
authenticate_client(connection);
while (!shared::should_exit) {
// Parse all new packets.
while (const auto packet = connection.recv_packet()) {
parse_packet(packet.value());
}
// Wait for localplayer to be constructed before doing anything.
if (::players.empty()) {
continue;
}
// Handle input, which may prime text input -> send it to the
// server.
update_input();
// Send our localplayer to the server.
if (::should_send_move) {
send_move_packet(connection);
::should_send_move = false;
}
update_chunks(connection);
update_state();
update_players();
client::draw::draw(::players, ::chunks);
if (!connection.good()) {
shared::print::notify("client: disconnected for \"" +
connection.get_bad_reason() + "\"\n");
shared::should_exit = true;
}
}
shared::print::notify("client: disconnecting from server\n");
client::render::quit();
client::settings::save();
}
} // namespace client
|