diff options
Diffstat (limited to 'game/server')
| -rw-r--r-- | game/server/chat.cc | 17 | ||||
| -rw-r--r-- | game/server/chat.hh | 8 | ||||
| -rw-r--r-- | game/server/game.cc | 3 | ||||
| -rw-r--r-- | game/server/globals.cc | 7 | ||||
| -rw-r--r-- | game/server/globals.hh | 6 | ||||
| -rw-r--r-- | game/server/main.cc | 10 | ||||
| -rw-r--r-- | game/server/overworld.cc | 59 | ||||
| -rw-r--r-- | game/server/overworld.hh | 20 | ||||
| -rw-r--r-- | game/server/receive.cc | 22 | ||||
| -rw-r--r-- | game/server/sessions.cc | 104 | ||||
| -rw-r--r-- | game/server/sessions.hh | 20 | ||||
| -rw-r--r-- | game/server/status.cc | 3 | ||||
| -rw-r--r-- | game/server/universe.cc | 25 | ||||
| -rw-r--r-- | game/server/universe.hh | 6 | ||||
| -rw-r--r-- | game/server/unloader.cc | 15 | ||||
| -rw-r--r-- | game/server/unloader.hh | 2 | ||||
| -rw-r--r-- | game/server/whitelist.cc | 16 | ||||
| -rw-r--r-- | game/server/whitelist.hh | 4 | ||||
| -rw-r--r-- | game/server/worldgen.cc | 17 | ||||
| -rw-r--r-- | game/server/worldgen.hh | 4 |
20 files changed, 195 insertions, 173 deletions
diff --git a/game/server/chat.cc b/game/server/chat.cc index 709d21b..1634c59 100644 --- a/game/server/chat.cc +++ b/game/server/chat.cc @@ -1,16 +1,19 @@ #include "server/pch.hh" + #include "server/chat.hh" #include "server/globals.hh" #include "server/sessions.hh" #include "shared/protocol.hh" -static void on_chat_message_packet(const protocol::ChatMessage &packet) +static void on_chat_message_packet(const protocol::ChatMessage& packet) { if(packet.type == protocol::ChatMessage::TEXT_MESSAGE) { - if(auto session = sessions::find(packet.peer)) + if(auto session = sessions::find(packet.peer)) { server_chat::broadcast(packet.message.c_str(), session->client_username.c_str()); - else server_chat::broadcast(packet.message.c_str(), packet.sender.c_str()); + } else { + server_chat::broadcast(packet.message.c_str(), packet.sender.c_str()); + } } } @@ -19,12 +22,12 @@ void server_chat::init(void) globals::dispatcher.sink<protocol::ChatMessage>().connect<&on_chat_message_packet>(); } -void server_chat::broadcast(const char *message) +void server_chat::broadcast(const char* message) { server_chat::broadcast(message, "server"); } -void server_chat::broadcast(const char *message, const char *sender) +void server_chat::broadcast(const char* message, const char* sender) { protocol::ChatMessage packet; packet.type = protocol::ChatMessage::TEXT_MESSAGE; @@ -36,12 +39,12 @@ void server_chat::broadcast(const char *message, const char *sender) spdlog::info("<{}> {}", sender, message); } -void server_chat::send(Session *session, const char *message) +void server_chat::send(Session* session, const char* message) { server_chat::send(session, message, "server"); } -void server_chat::send(Session *session, const char *message, const char *sender) +void server_chat::send(Session* session, const char* message, const char* sender) { protocol::ChatMessage packet; packet.type = protocol::ChatMessage::TEXT_MESSAGE; diff --git a/game/server/chat.hh b/game/server/chat.hh index 7efc7ff..2557290 100644 --- a/game/server/chat.hh +++ b/game/server/chat.hh @@ -7,10 +7,10 @@ struct Session; namespace server_chat { void init(void); -void broadcast(const char *message); -void broadcast(const char *message, const char *sender); -void send(Session *session, const char *message); -void send(Session *session, const char *message, const char *sender); +void broadcast(const char* message); +void broadcast(const char* message, const char* sender); +void send(Session* session, const char* message); +void send(Session* session, const char* message, const char* sender); } // namespace server_chat #endif /* SERVER_CHAT_HH */ diff --git a/game/server/game.cc b/game/server/game.cc index c6fc67f..978474c 100644 --- a/game/server/game.cc +++ b/game/server/game.cc @@ -1,4 +1,5 @@ #include "server/pch.hh" + #include "server/game.hh" #include "core/cmdline.hh" @@ -75,7 +76,7 @@ void server_game::init_late(void) address.port = listen_port.get_value(); globals::server_host = enet_host_create(&address, sessions::max_players.get_value() + status_peers.get_value(), 1, 0, 0); - + if(!globals::server_host) { spdlog::critical("game: unable to setup an ENet host"); std::terminate(); diff --git a/game/server/globals.cc b/game/server/globals.cc index e0f50da..883588b 100644 --- a/game/server/globals.cc +++ b/game/server/globals.cc @@ -1,4 +1,5 @@ #include "server/pch.hh" + #include "server/globals.hh" #include "core/config.hh" @@ -7,11 +8,11 @@ Config globals::server_config; -ENetHost *globals::server_host; +ENetHost* globals::server_host; bool globals::is_running; unsigned int globals::tickrate; std::uint64_t globals::tickrate_dt; -Dimension *globals::spawn_dimension; -std::unordered_map<std::string, Dimension *> globals::dimensions; +Dimension* globals::spawn_dimension; +std::unordered_map<std::string, Dimension*> globals::dimensions; diff --git a/game/server/globals.hh b/game/server/globals.hh index f2fc256..54f025a 100644 --- a/game/server/globals.hh +++ b/game/server/globals.hh @@ -12,14 +12,14 @@ namespace globals { extern Config server_config; -extern ENetHost *server_host; +extern ENetHost* server_host; extern bool is_running; extern unsigned int tickrate; extern std::uint64_t tickrate_dt; -extern Dimension *spawn_dimension; -extern std::unordered_map<std::string, Dimension *> dimensions; +extern Dimension* spawn_dimension; +extern std::unordered_map<std::string, Dimension*> dimensions; } // namespace globals #endif /* SERVER_GLOBALS_HH */ diff --git a/game/server/main.cc b/game/server/main.cc index 0eff05f..42e76f4 100644 --- a/game/server/main.cc +++ b/game/server/main.cc @@ -24,7 +24,7 @@ static void on_termination_signal(int) globals::is_running = false; } -int main(int argc, char **argv) +int main(int argc, char** argv) { cmdline::create(argc, argv); @@ -58,7 +58,7 @@ int main(int argc, char **argv) server_game::init_late(); std::uint64_t last_curtime = globals::curtime; - + while(globals::is_running) { globals::curtime = epoch::microseconds(); @@ -68,12 +68,12 @@ int main(int argc, char **argv) globals::fixed_frametime_avg *= 0.5f; last_curtime = globals::curtime; - + server_game::fixed_update(); server_game::fixed_update_late(); globals::dispatcher.update(); - + globals::fixed_framecount += 1; std::this_thread::sleep_for(std::chrono::microseconds(globals::tickrate_dt)); @@ -85,7 +85,7 @@ int main(int argc, char **argv) } server_game::deinit(); - + resource::hard_cleanup<BinFile>(); resource::hard_cleanup<Image>(); diff --git a/game/server/overworld.cc b/game/server/overworld.cc index 29bfd64..7021717 100644 --- a/game/server/overworld.cc +++ b/game/server/overworld.cc @@ -1,4 +1,5 @@ #include "server/pch.hh" + #include "server/overworld.hh" #include "core/vectors.hh" @@ -8,7 +9,7 @@ #include "shared/voxel_storage.hh" // FIXME: load these from a file -static void compute_tree_feature(unsigned int height, Feature &feature, voxel_id log_voxel, voxel_id leaves_voxel) +static void compute_tree_feature(unsigned int height, Feature& feature, voxel_id log_voxel, voxel_id leaves_voxel) { // Ensure the tree height is too small height = cxpr::max<unsigned int>(height, 4U); @@ -68,7 +69,7 @@ static void compute_tree_feature(unsigned int height, Feature &feature, voxel_id } } -Overworld::Overworld(const char *name) : Dimension(name, -30.0f) +Overworld::Overworld(const char* name) : Dimension(name, -30.0f) { m_bottommost_chunk.set_limits(-64, -4); m_terrain_variation.set_limits(16, 256); @@ -79,7 +80,7 @@ Overworld::Overworld(const char *name) : Dimension(name, -30.0f) compute_tree_feature(8U, m_feat_tree[3], game_voxels::oak_log, game_voxels::oak_leaves); } -void Overworld::init(Config &config) +void Overworld::init(Config& config) { m_terrain_variation.set_value(64); m_bottommost_chunk.set_value(-4); @@ -126,7 +127,7 @@ void Overworld::init_late(std::uint64_t global_seed) m_metamap.clear(); } -bool Overworld::generate(const chunk_pos &cpos, VoxelStorage &voxels) +bool Overworld::generate(const chunk_pos& cpos, VoxelStorage& voxels) { if(cpos.y <= m_bottommost_chunk.get_value()) { // If the player asks the generator @@ -157,14 +158,14 @@ bool Overworld::generate(const chunk_pos &cpos, VoxelStorage &voxels) return true; } -bool Overworld::is_inside_cave(const voxel_pos &vpos) +bool Overworld::is_inside_cave(const voxel_pos& vpos) { auto noise_a = fnlGetNoise3D(&m_fnl_caves_a, vpos.x, vpos.y * 2.0f, vpos.z); auto noise_b = fnlGetNoise3D(&m_fnl_caves_b, vpos.x, vpos.y * 2.0f, vpos.z); return (noise_a > 0.95f) && (noise_b > 0.85f); } -bool Overworld::is_inside_terrain(const voxel_pos &vpos) +bool Overworld::is_inside_terrain(const voxel_pos& vpos) { auto variation_noise = fnlGetNoise3D(&m_fnl_terrain, vpos.x, vpos.y, vpos.z); auto variation = m_terrain_variation.get_value() * (1.0f - (variation_noise * variation_noise)); @@ -172,7 +173,7 @@ bool Overworld::is_inside_terrain(const voxel_pos &vpos) return noise > 0.0f; } -const Overworld_Metadata &Overworld::get_or_create_metadata(const chunk_pos_xz &cpos) +const Overworld_Metadata& Overworld::get_or_create_metadata(const chunk_pos_xz& cpos) { auto it = m_metamap.find(cpos); @@ -181,7 +182,7 @@ const Overworld_Metadata &Overworld::get_or_create_metadata(const chunk_pos_xz & return it->second; } - auto &metadata = m_metamap.insert_or_assign(cpos, Overworld_Metadata()).first->second; + auto& metadata = m_metamap.insert_or_assign(cpos, Overworld_Metadata()).first->second; metadata.entropy.fill(std::numeric_limits<std::uint64_t>::max()); metadata.heightmap.fill(std::numeric_limits<voxel_pos::value_type>::min()); @@ -219,7 +220,7 @@ const Overworld_Metadata &Overworld::get_or_create_metadata(const chunk_pos_xz & auto lpos = local_pos((twister() % CHUNK_SIZE), (twister() % OW_NUM_TREES), (twister() % CHUNK_SIZE)); auto is_unique = true; - for(const auto &check_lpos : metadata.trees) { + for(const auto& check_lpos : metadata.trees) { if(cxvectors::distance2(check_lpos, lpos) <= 9) { is_unique = false; break; @@ -234,9 +235,9 @@ const Overworld_Metadata &Overworld::get_or_create_metadata(const chunk_pos_xz & return metadata; } -void Overworld::generate_terrain(const chunk_pos &cpos, VoxelStorage &voxels) +void Overworld::generate_terrain(const chunk_pos& cpos, VoxelStorage& voxels) { - auto &metadata = get_or_create_metadata(chunk_pos_xz(cpos.x, cpos.z)); + auto& metadata = get_or_create_metadata(chunk_pos_xz(cpos.x, cpos.z)); auto variation = m_terrain_variation.get_value(); for(unsigned long i = 0; i < CHUNK_VOLUME; ++i) { @@ -260,9 +261,9 @@ void Overworld::generate_terrain(const chunk_pos &cpos, VoxelStorage &voxels) } } -void Overworld::generate_surface(const chunk_pos &cpos, VoxelStorage &voxels) +void Overworld::generate_surface(const chunk_pos& cpos, VoxelStorage& voxels) { - auto &metadata = get_or_create_metadata(chunk_pos_xz(cpos.x, cpos.z)); + auto& metadata = get_or_create_metadata(chunk_pos_xz(cpos.x, cpos.z)); auto variation = m_terrain_variation.get_value(); for(unsigned long i = 0; i < CHUNK_VOLUME; ++i) { @@ -289,31 +290,35 @@ void Overworld::generate_surface(const chunk_pos &cpos, VoxelStorage &voxels) auto d_index = coord::to_index(d_lpos); if(d_lpos.y >= CHUNK_SIZE) { - if(!is_inside_terrain(d_vpos)) + if(!is_inside_terrain(d_vpos)) { break; + } + depth += 1U; - } - else { - if(voxels[d_index] == NULL_VOXEL_ID) + } else { + if(voxels[d_index] == NULL_VOXEL_ID) { break; + } + depth += 1U; } } if(depth < 5U) { - if(depth == 0U) + if(depth == 0U) { voxels[i] = game_voxels::grass; - else voxels[i] = game_voxels::dirt; + } else { + voxels[i] = game_voxels::dirt; + } } } - } -void Overworld::generate_caves(const chunk_pos &cpos, VoxelStorage &voxels) +void Overworld::generate_caves(const chunk_pos& cpos, VoxelStorage& voxels) { - auto &metadata = get_or_create_metadata(chunk_pos_xz(cpos.x, cpos.z)); + auto& metadata = get_or_create_metadata(chunk_pos_xz(cpos.x, cpos.z)); auto variation = m_terrain_variation.get_value(); - + for(unsigned long i = 0U; i < CHUNK_VOLUME; ++i) { auto lpos = coord::to_local(i); auto vpos = coord::to_voxel(cpos, lpos); @@ -331,7 +336,7 @@ void Overworld::generate_caves(const chunk_pos &cpos, VoxelStorage &voxels) } } -void Overworld::generate_features(const chunk_pos &cpos, VoxelStorage &voxels) +void Overworld::generate_features(const chunk_pos& cpos, VoxelStorage& voxels) { const chunk_pos_xz tree_chunks[] = { chunk_pos_xz(cpos.x - 0, cpos.z - 1), @@ -346,10 +351,10 @@ void Overworld::generate_features(const chunk_pos &cpos, VoxelStorage &voxels) }; for(unsigned int i = 0U; i < cxpr::array_size(tree_chunks); ++i) { - const auto &cpos_xz = tree_chunks[i]; - const auto &metadata = get_or_create_metadata(cpos_xz); + const auto& cpos_xz = tree_chunks[i]; + const auto& metadata = get_or_create_metadata(cpos_xz); - for(const auto &tree_info : metadata.trees) { + for(const auto& tree_info : metadata.trees) { auto hdx = static_cast<std::size_t>(tree_info.x + tree_info.z * CHUNK_SIZE); auto height = metadata.heightmap[hdx]; diff --git a/game/server/overworld.hh b/game/server/overworld.hh index 979e5a3..972a91d 100644 --- a/game/server/overworld.hh +++ b/game/server/overworld.hh @@ -18,26 +18,26 @@ struct Overworld_Metadata final { class Overworld final : public Dimension { public: - explicit Overworld(const char *name); + explicit Overworld(const char* name); virtual ~Overworld(void) = default; public: - virtual void init(Config &config) override; + virtual void init(Config& config) override; virtual void init_late(std::uint64_t global_seed) override; - virtual bool generate(const chunk_pos &cpos, VoxelStorage &voxels) override; + virtual bool generate(const chunk_pos& cpos, VoxelStorage& voxels) override; private: - bool is_inside_cave(const voxel_pos &vpos); - bool is_inside_terrain(const voxel_pos &vpos); + bool is_inside_cave(const voxel_pos& vpos); + bool is_inside_terrain(const voxel_pos& vpos); private: - const Overworld_Metadata &get_or_create_metadata(const chunk_pos_xz &cpos); + const Overworld_Metadata& get_or_create_metadata(const chunk_pos_xz& cpos); private: - void generate_terrain(const chunk_pos &cpos, VoxelStorage &voxels); - void generate_surface(const chunk_pos &cpos, VoxelStorage &voxels); - void generate_caves(const chunk_pos &cpos, VoxelStorage &voxels); - void generate_features(const chunk_pos &cpos, VoxelStorage &voxels); + void generate_terrain(const chunk_pos& cpos, VoxelStorage& voxels); + void generate_surface(const chunk_pos& cpos, VoxelStorage& voxels); + void generate_caves(const chunk_pos& cpos, VoxelStorage& voxels); + void generate_features(const chunk_pos& cpos, VoxelStorage& voxels); private: ConfigInt m_terrain_variation; diff --git a/game/server/receive.cc b/game/server/receive.cc index 780ae53..7674122 100644 --- a/game/server/receive.cc +++ b/game/server/receive.cc @@ -1,4 +1,5 @@ #include "server/pch.hh" + #include "server/receive.hh" #include "core/config.hh" @@ -18,11 +19,11 @@ #include "server/universe.hh" #include "server/worldgen.hh" -static void on_entity_transform_packet(const protocol::EntityTransform &packet) +static void on_entity_transform_packet(const protocol::EntityTransform& packet) { if(auto session = sessions::find(packet.peer)) { if(session->dimension && session->dimension->entities.valid(session->player_entity)) { - auto &component = session->dimension->entities.emplace_or_replace<TransformComponent>(session->player_entity); + auto& component = session->dimension->entities.emplace_or_replace<TransformComponent>(session->player_entity); component.angles = packet.angles; component.chunk = packet.chunk; component.local = packet.local; @@ -40,11 +41,11 @@ static void on_entity_transform_packet(const protocol::EntityTransform &packet) } } -static void on_entity_velocity_packet(const protocol::EntityVelocity &packet) +static void on_entity_velocity_packet(const protocol::EntityVelocity& packet) { if(auto session = sessions::find(packet.peer)) { if(session->dimension && session->dimension->entities.valid(session->player_entity)) { - auto &component = session->dimension->entities.emplace_or_replace<VelocityComponent>(session->player_entity); + auto& component = session->dimension->entities.emplace_or_replace<VelocityComponent>(session->player_entity); component.value = packet.value; protocol::EntityVelocity response; @@ -58,11 +59,11 @@ static void on_entity_velocity_packet(const protocol::EntityVelocity &packet) } } -static void on_entity_head_packet(const protocol::EntityHead &packet) +static void on_entity_head_packet(const protocol::EntityHead& packet) { if(auto session = sessions::find(packet.peer)) { if(session->dimension && session->dimension->entities.valid(session->player_entity)) { - auto &component = session->dimension->entities.emplace_or_replace<HeadComponent>(session->player_entity); + auto& component = session->dimension->entities.emplace_or_replace<HeadComponent>(session->player_entity); component.angles = packet.angles; protocol::EntityHead response; @@ -76,7 +77,7 @@ static void on_entity_head_packet(const protocol::EntityHead &packet) } } -static void on_set_voxel_packet(const protocol::SetVoxel &packet) +static void on_set_voxel_packet(const protocol::SetVoxel& packet) { if(auto session = sessions::find(packet.peer)) { if(session->dimension && !session->dimension->set_voxel(packet.voxel, packet.vpos)) { @@ -112,7 +113,7 @@ static void on_set_voxel_packet(const protocol::SetVoxel &packet) } } -static void on_request_chunk_packet(const protocol::RequestChunk &packet) +static void on_request_chunk_packet(const protocol::RequestChunk& packet) { if(auto session = sessions::find(packet.peer)) { if(!session->dimension || !session->dimension->entities.valid(session->player_entity)) { @@ -132,8 +133,7 @@ static void on_request_chunk_packet(const protocol::RequestChunk &packet) response.chunk = packet.cpos; response.voxels = chunk->get_voxels(); protocol::send(packet.peer, protocol::encode(response)); - } - else { + } else { worldgen::request_chunk(session, packet.cpos); } } @@ -141,7 +141,7 @@ static void on_request_chunk_packet(const protocol::RequestChunk &packet) } } -static void on_entity_sound_packet(const protocol::EntitySound &packet) +static void on_entity_sound_packet(const protocol::EntitySound& packet) { if(auto session = sessions::find(packet.peer)) { if(!session->dimension || !session->dimension->entities.valid(session->player_entity)) { diff --git a/game/server/sessions.cc b/game/server/sessions.cc index 58f12d4..a8745cb 100644 --- a/game/server/sessions.cc +++ b/game/server/sessions.cc @@ -1,4 +1,5 @@ #include "server/pch.hh" + #include "server/sessions.hh" #include "core/config.hh" @@ -24,22 +25,22 @@ class DimensionListener final { public: - explicit DimensionListener(Dimension *dimension); - void on_destroy_entity(const entt::registry ®istry, entt::entity entity); + explicit DimensionListener(Dimension* dimension); + void on_destroy_entity(const entt::registry& registry, entt::entity entity); private: - Dimension *dimension; + Dimension* dimension; }; ConfigUnsigned sessions::max_players(8U, 1U, 128U); unsigned int sessions::num_players = 0U; -static emhash8::HashMap<std::string, Session *> username_map; -static emhash8::HashMap<std::uint64_t, Session *> identity_map; +static emhash8::HashMap<std::string, Session*> username_map; +static emhash8::HashMap<std::uint64_t, Session*> identity_map; static std::vector<DimensionListener> dimension_listeners; static std::vector<Session> sessions_vector; -static void on_login_request_packet(const protocol::LoginRequest &packet) +static void on_login_request_packet(const protocol::LoginRequest& packet) { if(packet.version > protocol::VERSION) { protocol::Disconnect response; @@ -95,15 +96,14 @@ static void on_login_request_packet(const protocol::LoginRequest &packet) protocol::send(packet.peer, protocol::encode(response)); return; } - } - else if(packet.password_hash != server_game::password_hash) { + } else if(packet.password_hash != server_game::password_hash) { protocol::Disconnect response; response.reason = "protocol.password_incorrect"; protocol::send(packet.peer, protocol::encode(response)); return; } - if(Session *session = sessions::create(packet.peer, packet.username.c_str())) { + if(Session* session = sessions::create(packet.peer, packet.username.c_str())) { protocol::LoginResponse response; response.client_index = session->client_index; response.client_identity = session->client_identity; @@ -114,9 +114,12 @@ static void on_login_request_packet(const protocol::LoginRequest &packet) dim_info.name = globals::spawn_dimension->get_name(); dim_info.gravity = globals::spawn_dimension->get_gravity(); protocol::send(packet.peer, protocol::encode(dim_info)); - - spdlog::info("sessions: {} [{}] logged in with client_index={} in {}", session->client_username, - session->client_identity, session->client_index, globals::spawn_dimension->get_name()); + + spdlog::info("sessions: {} [{}] logged in with client_index={} in {}", + session->client_username, + session->client_identity, + session->client_index, + globals::spawn_dimension->get_name()); // FIXME: only send entities that are present within the current // player's view bounding box; this also would mean we're not sending @@ -157,9 +160,9 @@ static void on_login_request_packet(const protocol::LoginRequest &packet) session->player_entity = globals::spawn_dimension->entities.create(); shared_factory::create_player(globals::spawn_dimension, session->player_entity); - const auto &head = globals::spawn_dimension->entities.get<HeadComponent>(session->player_entity); - const auto &transform = globals::spawn_dimension->entities.get<TransformComponent>(session->player_entity); - const auto &velocity = globals::spawn_dimension->entities.get<VelocityComponent>(session->player_entity); + const auto& head = globals::spawn_dimension->entities.get<HeadComponent>(session->player_entity); + const auto& transform = globals::spawn_dimension->entities.get<TransformComponent>(session->player_entity); + const auto& velocity = globals::spawn_dimension->entities.get<VelocityComponent>(session->player_entity); protocol::EntityHead head_packet; head_packet.entity = session->player_entity; @@ -209,14 +212,14 @@ static void on_login_request_packet(const protocol::LoginRequest &packet) protocol::send(packet.peer, protocol::encode(response)); } -static void on_disconnect_packet(const protocol::Disconnect &packet) +static void on_disconnect_packet(const protocol::Disconnect& packet) { - if(Session *session = sessions::find(packet.peer)) { + if(Session* session = sessions::find(packet.peer)) { protocol::ChatMessage message; message.type = protocol::ChatMessage::PLAYER_LEAVE; message.sender = session->client_username; message.message = packet.reason; - + protocol::broadcast(globals::server_host, protocol::encode(message), session->peer); spdlog::info("{} disconnected ({})", session->client_username, packet.reason); @@ -229,7 +232,7 @@ static void on_disconnect_packet(const protocol::Disconnect &packet) // NOTE: [sessions] is a good place for this since [receive] // handles entity data sent by players and [sessions] handles // everything else network related that is not player movement -static void on_voxel_set(const VoxelSetEvent &event) +static void on_voxel_set(const VoxelSetEvent& event) { protocol::SetVoxel packet; packet.vpos = coord::to_voxel(event.cpos, event.lpos); @@ -238,12 +241,12 @@ static void on_voxel_set(const VoxelSetEvent &event) protocol::broadcast(globals::server_host, protocol::encode(packet)); } -DimensionListener::DimensionListener(Dimension *dimension) +DimensionListener::DimensionListener(Dimension* dimension) { this->dimension = dimension; } -void DimensionListener::on_destroy_entity(const entt::registry ®istry, entt::entity entity) +void DimensionListener::on_destroy_entity(const entt::registry& registry, entt::entity entity) { protocol::RemoveEntity packet; packet.entity = entity; @@ -279,7 +282,7 @@ void sessions::init_late(void) void sessions::init_post_universe(void) { - for(auto &dimension : globals::dimensions) { + for(auto& dimension : globals::dimensions) { dimension_listeners.push_back(DimensionListener(dimension.second)); dimension.second->entities.on_destroy<entt::entity>().connect<&DimensionListener::on_destroy_entity>(dimension_listeners.back()); } @@ -293,7 +296,7 @@ void sessions::deinit(void) dimension_listeners.clear(); } -Session *sessions::create(ENetPeer *peer, const char *client_username) +Session* sessions::create(ENetPeer* peer, const char* client_username) { for(unsigned int i = 0U; i < sessions::max_players.get_value(); ++i) { if(!sessions_vector[i].peer) { @@ -303,7 +306,7 @@ Session *sessions::create(ENetPeer *peer, const char *client_username) sessions_vector[i].client_identity = client_identity; sessions_vector[i].client_username = client_username; sessions_vector[i].player_entity = entt::null; - sessions_vector[i].peer = peer; + sessions_vector[i].peer = peer; username_map[client_username] = &sessions_vector[i]; identity_map[client_identity] = &sessions_vector[i]; @@ -319,48 +322,57 @@ Session *sessions::create(ENetPeer *peer, const char *client_username) return nullptr; } -Session *sessions::find(const char *client_username) +Session* sessions::find(const char* client_username) { const auto it = username_map.find(client_username); - if(it != username_map.cend()) + if(it != username_map.cend()) { return it->second; - return nullptr; + } else { + return nullptr; + } } -Session *sessions::find(std::uint16_t client_index) +Session* sessions::find(std::uint16_t client_index) { if(client_index < sessions_vector.size()) { - if(!sessions_vector[client_index].peer) + if(!sessions_vector[client_index].peer) { return nullptr; - return &sessions_vector[client_index]; + } else { + return &sessions_vector[client_index]; + } } return nullptr; } -Session *sessions::find(std::uint64_t client_identity) +Session* sessions::find(std::uint64_t client_identity) { const auto it = identity_map.find(client_identity); - if(it != identity_map.cend()) + + if(it != identity_map.cend()) { return it->second; - return nullptr; + } else { + return nullptr; + } } -Session *sessions::find(ENetPeer *peer) +Session* sessions::find(ENetPeer* peer) { - if(peer != nullptr) - return reinterpret_cast<Session *>(peer->data); - return nullptr; + if(peer != nullptr) { + return reinterpret_cast<Session*>(peer->data); + } else { + return nullptr; + } } -void sessions::destroy(Session *session) +void sessions::destroy(Session* session) { if(session) { if(session->peer) { // Make sure we don't leave a mark session->peer->data = nullptr; } - + if(session->dimension) { session->dimension->entities.destroy(session->player_entity); } @@ -378,18 +390,18 @@ void sessions::destroy(Session *session) } } -void sessions::broadcast(const Dimension *dimension, ENetPacket *packet) +void sessions::broadcast(const Dimension* dimension, ENetPacket* packet) { - for(const auto &session : sessions_vector) { + for(const auto& session : sessions_vector) { if(session.peer && (session.dimension == dimension)) { enet_peer_send(session.peer, protocol::CHANNEL, packet); } } } -void sessions::broadcast(const Dimension *dimension, ENetPacket *packet, ENetPeer *except) +void sessions::broadcast(const Dimension* dimension, ENetPacket* packet, ENetPeer* except) { - for(const auto &session : sessions_vector) { + for(const auto& session : sessions_vector) { if(session.peer && (session.peer != except)) { enet_peer_send(session.peer, protocol::CHANNEL, packet); } @@ -401,9 +413,9 @@ void sessions::refresh_scoreboard(void) protocol::ScoreboardUpdate packet; for(std::size_t i = 0; i < sessions::max_players.get_value(); ++i) { - if(!sessions_vector[i].peer) - continue; - packet.names.push_back(sessions_vector[i].client_username); + if(sessions_vector[i].peer) { + packet.names.push_back(sessions_vector[i].client_username); + } } protocol::broadcast(globals::server_host, protocol::encode(packet)); diff --git a/game/server/sessions.hh b/game/server/sessions.hh index cb31bd9..b9a6348 100644 --- a/game/server/sessions.hh +++ b/game/server/sessions.hh @@ -11,8 +11,8 @@ struct Session final { std::uint64_t client_identity; std::string client_username; entt::entity player_entity; - Dimension *dimension; - ENetPeer *peer; + Dimension* dimension; + ENetPeer* peer; }; namespace sessions @@ -31,18 +31,18 @@ void deinit(void); namespace sessions { -Session *create(ENetPeer *peer, const char *client_username); -Session *find(const char *client_username); -Session *find(std::uint16_t client_index); -Session *find(std::uint64_t client_identity); -Session *find(ENetPeer *peer); -void destroy(Session *session); +Session* create(ENetPeer* peer, const char* client_username); +Session* find(const char* client_username); +Session* find(std::uint16_t client_index); +Session* find(std::uint64_t client_identity); +Session* find(ENetPeer* peer); +void destroy(Session* session); } // namespace sessions namespace sessions { -void broadcast(const Dimension *dimension, ENetPacket *packet); -void broadcast(const Dimension *dimension, ENetPacket *packet, ENetPeer *except); +void broadcast(const Dimension* dimension, ENetPacket* packet); +void broadcast(const Dimension* dimension, ENetPacket* packet, ENetPeer* except); } // namespace sessions namespace sessions diff --git a/game/server/status.cc b/game/server/status.cc index e44a4f4..6b64719 100644 --- a/game/server/status.cc +++ b/game/server/status.cc @@ -1,4 +1,5 @@ #include "server/pch.hh" + #include "server/status.hh" #include "core/config.hh" @@ -9,7 +10,7 @@ #include "server/globals.hh" #include "server/sessions.hh" -static void on_status_request_packet(const protocol::StatusRequest &packet) +static void on_status_request_packet(const protocol::StatusRequest& packet) { protocol::StatusResponse response; response.version = protocol::VERSION; diff --git a/game/server/universe.cc b/game/server/universe.cc index 22abaff..e7b8b0f 100644 --- a/game/server/universe.cc +++ b/game/server/universe.cc @@ -1,9 +1,10 @@ #include "server/pch.hh" + #include "server/universe.hh" +#include "core/buffer.hh" #include "core/config.hh" #include "core/epoch.hh" -#include "core/buffer.hh" #include "shared/chunk.hh" #include "shared/dimension.hh" @@ -25,9 +26,9 @@ static ConfigUnsigned64 universe_config_seed; static ConfigString universe_spawn_dimension("world"); static std::string universe_config_path; -static std::unordered_map<Dimension *, DimensionMetadata *> metadata_map; +static std::unordered_map<Dimension*, DimensionMetadata*> metadata_map; -static std::string make_chunk_filename(const DimensionMetadata *metadata, const chunk_pos &cpos) +static std::string make_chunk_filename(const DimensionMetadata* metadata, const chunk_pos& cpos) { const auto unsigned_x = static_cast<std::uint32_t>(cpos.x); const auto unsigned_y = static_cast<std::uint32_t>(cpos.y); @@ -35,7 +36,7 @@ static std::string make_chunk_filename(const DimensionMetadata *metadata, const return fmt::format("{}/{:08X}-{:08X}-{:08X}.zvox", metadata->zvox_dir, unsigned_x, unsigned_y, unsigned_z); } -static void add_new_dimension(Dimension *dimension) +static void add_new_dimension(Dimension* dimension) { if(globals::dimensions.count(dimension->get_name())) { spdlog::critical("universe: dimension named {} already exists", dimension->get_name()); @@ -57,10 +58,10 @@ static void add_new_dimension(Dimension *dimension) spdlog::critical("universe: {}: {}", metadata->zvox_dir, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); std::terminate(); } - + globals::dimensions.insert_or_assign(dimension->get_name(), dimension); - - auto &mapped_metadata = metadata_map.insert_or_assign(dimension, metadata).first->second; + + auto& mapped_metadata = metadata_map.insert_or_assign(dimension, metadata).first->second; dimension->init(mapped_metadata->config); @@ -69,7 +70,7 @@ static void add_new_dimension(Dimension *dimension) dimension->init_late(universe_config_seed.get_value()); } -static void internal_save_chunk(const DimensionMetadata *metadata, const Dimension *dimension, const chunk_pos &cpos, const Chunk *chunk) +static void internal_save_chunk(const DimensionMetadata* metadata, const Dimension* dimension, const chunk_pos& cpos, const Chunk* chunk) { auto path = make_chunk_filename(metadata, cpos); @@ -140,14 +141,14 @@ void universe::deinit(void) universe::save_all_chunks(dimension.second); delete dimension.second; } - + globals::dimensions.clear(); globals::spawn_dimension = nullptr; universe_config.save_file(universe_config_path.c_str()); } -Chunk *universe::load_chunk(Dimension *dimension, const chunk_pos &cpos) +Chunk* universe::load_chunk(Dimension* dimension, const chunk_pos& cpos) { if(auto chunk = dimension->find_chunk(cpos)) { // Just return the existing chunk which is @@ -182,7 +183,7 @@ Chunk *universe::load_chunk(Dimension *dimension, const chunk_pos &cpos) return nullptr; } -void universe::save_chunk(Dimension *dimension, const chunk_pos &cpos) +void universe::save_chunk(Dimension* dimension, const chunk_pos& cpos) { auto metadata = metadata_map.find(dimension); @@ -197,7 +198,7 @@ void universe::save_chunk(Dimension *dimension, const chunk_pos &cpos) } } -void universe::save_all_chunks(Dimension *dimension) +void universe::save_all_chunks(Dimension* dimension) { auto group = dimension->chunks.group(entt::get<ChunkComponent, InhabitedComponent>); auto metadata = metadata_map.find(dimension); diff --git a/game/server/universe.hh b/game/server/universe.hh index 00c8b8d..2a16806 100644 --- a/game/server/universe.hh +++ b/game/server/universe.hh @@ -17,9 +17,9 @@ void deinit(void); namespace universe { -Chunk *load_chunk(Dimension *dimension, const chunk_pos &cpos); -void save_chunk(Dimension *dimension, const chunk_pos &cpos); -void save_all_chunks(Dimension *dimension); +Chunk* load_chunk(Dimension* dimension, const chunk_pos& cpos); +void save_chunk(Dimension* dimension, const chunk_pos& cpos); +void save_all_chunks(Dimension* dimension); } // namespace universe #endif /* SERVER_UNIVERSE_HH */ diff --git a/game/server/unloader.cc b/game/server/unloader.cc index fd838d0..f986a61 100644 --- a/game/server/unloader.cc +++ b/game/server/unloader.cc @@ -1,13 +1,13 @@ #include "server/pch.hh" + #include "server/unloader.hh" #include "core/config.hh" -#include "shared/chunk_aabb.hh" #include "shared/chunk.hh" +#include "shared/chunk_aabb.hh" #include "shared/dimension.hh" #include "shared/player.hh" -#include "shared/player.hh" #include "shared/transform.hh" #include "server/game.hh" @@ -15,12 +15,12 @@ #include "server/inhabited.hh" #include "server/universe.hh" -static void on_chunk_update(const ChunkUpdateEvent &event) +static void on_chunk_update(const ChunkUpdateEvent& event) { event.dimension->chunks.emplace_or_replace<InhabitedComponent>(event.chunk->get_entity()); } -static void on_voxel_set(const VoxelSetEvent &event) +static void on_voxel_set(const VoxelSetEvent& event) { event.dimension->chunks.emplace_or_replace<InhabitedComponent>(event.chunk->get_entity()); } @@ -33,10 +33,9 @@ void unloader::init(void) void unloader::init_late(void) { - } -void unloader::fixed_update_late(Dimension *dimension) +void unloader::fixed_update_late(Dimension* dimension) { auto group = dimension->entities.group(entt::get<PlayerComponent, TransformComponent>); auto boxes = std::vector<ChunkAABB>(); @@ -47,14 +46,14 @@ void unloader::fixed_update_late(Dimension *dimension) aabb.max = transform.chunk + static_cast<chunk_pos::value_type>(server_game::view_distance.get_value()); boxes.push_back(aabb); } - + auto view = dimension->chunks.view<ChunkComponent>(); auto chunk_in_view = false; for(const auto [entity, chunk] : view.each()) { chunk_in_view = false; - for(const auto &aabb : boxes) { + for(const auto& aabb : boxes) { if(aabb.contains(chunk.cpos)) { chunk_in_view = true; break; diff --git a/game/server/unloader.hh b/game/server/unloader.hh index d7a95da..414cdc4 100644 --- a/game/server/unloader.hh +++ b/game/server/unloader.hh @@ -8,7 +8,7 @@ namespace unloader { void init(void); void init_late(void); -void fixed_update_late(Dimension *dimension); +void fixed_update_late(Dimension* dimension); } // namespace unloader #endif /* SERVER_UNLOADER_HH */ diff --git a/game/server/whitelist.cc b/game/server/whitelist.cc index 4d9d394..6635122 100644 --- a/game/server/whitelist.cc +++ b/game/server/whitelist.cc @@ -1,4 +1,5 @@ #include "server/pch.hh" + #include "server/whitelist.hh" #include "core/config.hh" @@ -8,7 +9,7 @@ #include "server/game.hh" #include "server/globals.hh" -constexpr static const char *DEFAULT_FILENAME = "whitelist.txt"; +constexpr static const char* DEFAULT_FILENAME = "whitelist.txt"; constexpr static char SEPARATOR_CHAR = ':'; ConfigBoolean whitelist::enabled(false); @@ -38,7 +39,7 @@ void whitelist::init_late(void) whitelist::filename.set(DEFAULT_FILENAME); } - PHYSFS_File *file = PHYSFS_openRead(whitelist::filename.get()); + PHYSFS_File* file = PHYSFS_openRead(whitelist::filename.get()); if(file == nullptr) { spdlog::warn("whitelist: {}: {}", whitelist::filename.get(), PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); @@ -61,8 +62,7 @@ void whitelist::init_late(void) // to the global server password; this allows easier adding // of guest accounts which can later be edited to use a better password whitelist_map[line] = server_game::password_hash; - } - else { + } else { const auto username = line.substr(0, location); const auto password = line.substr(location + 1); whitelist_map[username] = crc64::get(password); @@ -77,12 +77,12 @@ void whitelist::deinit(void) // UNDONE: implement saving } -bool whitelist::contains(const char *username) +bool whitelist::contains(const char* username) { return whitelist_map.contains(username); } -bool whitelist::matches(const char *username, std::uint64_t password_hash) +bool whitelist::matches(const char* username, std::uint64_t password_hash) { const auto it = whitelist_map.find(username); @@ -91,7 +91,5 @@ bool whitelist::matches(const char *username, std::uint64_t password_hash) return false; } - if(it->second == password_hash) - return true; - return false; + return it->second == password_hash; } diff --git a/game/server/whitelist.hh b/game/server/whitelist.hh index 6cbaa0b..fe7656b 100644 --- a/game/server/whitelist.hh +++ b/game/server/whitelist.hh @@ -20,8 +20,8 @@ void deinit(void); namespace whitelist { -bool contains(const char *username); -bool matches(const char *username, std::uint64_t password_hash); +bool contains(const char* username); +bool matches(const char* username, std::uint64_t password_hash); } // namespace whitelist #endif /* SERVER_WHITELIST_HH */ diff --git a/game/server/worldgen.cc b/game/server/worldgen.cc index ef3c11e..5c74d47 100644 --- a/game/server/worldgen.cc +++ b/game/server/worldgen.cc @@ -1,4 +1,5 @@ #include "server/pch.hh" + #include "server/worldgen.hh" #include "core/cmdline.hh" @@ -14,22 +15,22 @@ static bool aggressive_caching; -static emhash8::HashMap<Dimension *, emhash8::HashMap<chunk_pos, std::unordered_set<Session *>>> active_tasks; +static emhash8::HashMap<Dimension*, emhash8::HashMap<chunk_pos, std::unordered_set<Session*>>> active_tasks; class WorldgenTask final : public Task { public: - explicit WorldgenTask(Dimension *dimension, const chunk_pos &cpos); + explicit WorldgenTask(Dimension* dimension, const chunk_pos& cpos); virtual ~WorldgenTask(void) = default; virtual void process(void) override; virtual void finalize(void) override; private: - Dimension *m_dimension; + Dimension* m_dimension; VoxelStorage m_voxels; chunk_pos m_cpos; }; -WorldgenTask::WorldgenTask(Dimension *dimension, const chunk_pos &cpos) +WorldgenTask::WorldgenTask(Dimension* dimension, const chunk_pos& cpos) { m_dimension = dimension; m_voxels.fill(rand()); // trolling @@ -103,7 +104,7 @@ void worldgen::init(void) aggressive_caching = cmdline::contains("aggressive-caching"); } -bool worldgen::is_generating(Dimension *dimension, const chunk_pos &cpos) +bool worldgen::is_generating(Dimension* dimension, const chunk_pos& cpos) { auto dim_tasks = active_tasks.find(dimension); @@ -122,19 +123,19 @@ bool worldgen::is_generating(Dimension *dimension, const chunk_pos &cpos) return true; } -void worldgen::request_chunk(Session *session, const chunk_pos &cpos) +void worldgen::request_chunk(Session* session, const chunk_pos& cpos) { if(session->dimension) { auto dim_tasks = active_tasks.find(session->dimension); if(dim_tasks == active_tasks.cend()) { - dim_tasks = active_tasks.emplace(session->dimension, emhash8::HashMap<chunk_pos, std::unordered_set<Session *>>()).first; + dim_tasks = active_tasks.emplace(session->dimension, emhash8::HashMap<chunk_pos, std::unordered_set<Session*>>()).first; } auto it = dim_tasks->second.find(cpos); if(it == dim_tasks->second.cend()) { - auto &sessions = dim_tasks->second.insert_or_assign(cpos, std::unordered_set<Session *>()).first->second; + auto& sessions = dim_tasks->second.insert_or_assign(cpos, std::unordered_set<Session*>()).first->second; sessions.insert(session); threading::submit<WorldgenTask>(session->dimension, cpos); diff --git a/game/server/worldgen.hh b/game/server/worldgen.hh index 7fb08b9..b0127e5 100644 --- a/game/server/worldgen.hh +++ b/game/server/worldgen.hh @@ -14,8 +14,8 @@ void init(void); namespace worldgen { -bool is_generating(Dimension *dimension, const chunk_pos &cpos); -void request_chunk(Session *session, const chunk_pos &cpos); +bool is_generating(Dimension* dimension, const chunk_pos& cpos); +void request_chunk(Session* session, const chunk_pos& cpos); } // namespace worldgen #endif /* SERVER_WORLDGEN_HH */ |
