mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
refactor(gameplay): split world into client and server
Remove monolithic World, Player, Chunk classes. Introduce ClientWorld, ServerWorld, ClientPlayer, and related networked components. Add Abseil dependency for logging and checks. Rename old files to pre_remove_* and update all includes and references accordingly.
This commit is contained in:
@@ -20,7 +20,7 @@ endif()
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(Protobuf REQUIRED)
|
||||
|
||||
find_package(absl REQUIRED)
|
||||
|
||||
if (UNIX AND NOT APPLE)
|
||||
find_package(Freetype REQUIRED)
|
||||
@@ -110,11 +110,8 @@ add_executable(${PROJECT_NAME}
|
||||
src/config.cpp
|
||||
src/dev_panel.cpp
|
||||
src/gameplay/biome.cpp
|
||||
src/gameplay/chunk.cpp
|
||||
src/gameplay/chunk_generator.cpp
|
||||
src/gameplay/player.cpp
|
||||
src/gameplay/tree.cpp
|
||||
src/gameplay/world.cpp
|
||||
src/input.cpp
|
||||
src/map_table.cpp
|
||||
src/renderer.cpp
|
||||
@@ -208,6 +205,8 @@ target_link_libraries(${PROJECT_NAME}
|
||||
imgui
|
||||
tbb
|
||||
protobuf::libprotobuf
|
||||
absl::log
|
||||
absl::check
|
||||
)
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#pragma once
|
||||
#include "Cubed/gameplay/client_world.hpp"
|
||||
#include "Cubed/gameplay/network_server.hpp"
|
||||
#include "Cubed/gameplay/server_world.hpp"
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include "Cubed/camera.hpp"
|
||||
#include "Cubed/dev_panel.hpp"
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/renderer.hpp"
|
||||
#include "Cubed/texture_manager.hpp"
|
||||
#include "Cubed/window.hpp"
|
||||
@@ -36,14 +38,19 @@ public:
|
||||
Renderer& renderer();
|
||||
TextureManager& texture_manager();
|
||||
Window& window();
|
||||
World& world();
|
||||
ClientWorld& client_world();
|
||||
ServerWorld& server_world();
|
||||
|
||||
private:
|
||||
Camera m_camera;
|
||||
TextureManager m_texture_manager;
|
||||
World m_world;
|
||||
NetworkServer m_server;
|
||||
std::shared_ptr<NetworkClient> m_client;
|
||||
ClientWorld m_client_world;
|
||||
|
||||
DevPanel m_dev_panel{*this};
|
||||
Renderer m_renderer{m_camera, m_world, m_texture_manager, m_dev_panel};
|
||||
Renderer m_renderer{m_camera, m_client_world, m_texture_manager,
|
||||
m_dev_panel};
|
||||
|
||||
Window m_window{m_renderer};
|
||||
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
class Player;
|
||||
class ClientPlayer;
|
||||
|
||||
class Camera {
|
||||
private:
|
||||
bool m_firse_mouse = true;
|
||||
Player* m_player;
|
||||
ClientPlayer* m_player;
|
||||
float m_last_mouse_x, m_last_mouse_y;
|
||||
glm::vec3 m_camera_pos;
|
||||
bool m_under_water = false;
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
|
||||
void update_move_camera();
|
||||
|
||||
void camera_init(Player* player);
|
||||
void camera_init(ClientPlayer* player);
|
||||
void hot_reload();
|
||||
void reset_camera();
|
||||
void update_cursor_position_camera(double xpos, double ypos);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
namespace Cubed {
|
||||
|
||||
class App;
|
||||
class Player;
|
||||
class ClientPlayer;
|
||||
class DevPanel {
|
||||
struct ConfigView {
|
||||
float fov = 70.0f;
|
||||
@@ -38,7 +38,7 @@ public:
|
||||
private:
|
||||
App& m_app;
|
||||
ConfigView m_config;
|
||||
Player* m_player;
|
||||
ClientPlayer* m_player;
|
||||
PlayerProfile m_player_profile;
|
||||
TextEditing m_text_editing;
|
||||
bool m_need_save_config = false;
|
||||
|
||||
@@ -14,7 +14,7 @@ enum class Gait { WALK = 0, RUN };
|
||||
class ClientWorld;
|
||||
class ClientPlayer {
|
||||
public:
|
||||
ClientPlayer(ClientWorld& world, std::string_view name);
|
||||
ClientPlayer(ClientWorld& world);
|
||||
~ClientPlayer();
|
||||
AABB get_aabb() const;
|
||||
const glm::vec3& get_front() const;
|
||||
@@ -52,6 +52,8 @@ public:
|
||||
const std::string& get_uuid() const;
|
||||
const std::string& get_name() const;
|
||||
|
||||
void init(std::string_view name);
|
||||
|
||||
private:
|
||||
using enum GameMode;
|
||||
float m_max_walk_speed = DEFAULT_MAX_WALK_SPEED;
|
||||
@@ -98,7 +100,7 @@ private:
|
||||
std::string m_uuid;
|
||||
ClientWorld& m_world;
|
||||
|
||||
std::shared_mutex m_player_pos_mutex;
|
||||
mutable std::shared_mutex m_player_pos_mutex;
|
||||
|
||||
bool ray_cast(const glm::vec3& start, const glm::vec3& dir,
|
||||
glm::ivec3& block_pos, glm::vec3& normal,
|
||||
|
||||
@@ -12,10 +12,10 @@ namespace Cubed {
|
||||
|
||||
class ClientWorld {
|
||||
public:
|
||||
ClientWorld(std::string_view player_name,
|
||||
std::shared_ptr<NetworkClient> client);
|
||||
ClientWorld();
|
||||
~ClientWorld();
|
||||
void init();
|
||||
void init(std::string_view player_name,
|
||||
std::shared_ptr<NetworkClient> client);
|
||||
void update(float delta_time);
|
||||
const std::optional<LookBlock>&
|
||||
get_look_block_pos(const std::string& name) const;
|
||||
@@ -36,6 +36,9 @@ public:
|
||||
void rendering_distance(int rendering_distance);
|
||||
void start_client_thread(std::string_view uuid);
|
||||
void stop_client_thread();
|
||||
|
||||
void hot_reload();
|
||||
|
||||
std::vector<glm::vec4>& planes();
|
||||
std::vector<ChunkRenderSnapshot>& render_snapshots();
|
||||
glm::vec3 sunlight_dir() const;
|
||||
|
||||
@@ -13,7 +13,7 @@ public:
|
||||
NetworkClient(ClientWorld& world);
|
||||
~NetworkClient();
|
||||
void close();
|
||||
asio::awaitable<void> connect(std::string ip, int port);
|
||||
|
||||
void send(Packet packet);
|
||||
void start(std::string ip, int port = 25530);
|
||||
|
||||
@@ -26,7 +26,9 @@ private:
|
||||
std::vector<char> m_read_buffer;
|
||||
std::deque<Packet> m_write_queue;
|
||||
asio::strand<asio::io_context::executor_type> m_strand;
|
||||
asio::awaitable<void> connect(std::string ip, int port);
|
||||
asio::awaitable<void> read_loop();
|
||||
|
||||
std::atomic<bool> m_closed{false};
|
||||
|
||||
// ClientWorld is managed by App
|
||||
|
||||
@@ -16,8 +16,7 @@ public:
|
||||
void start_server();
|
||||
|
||||
int port() const;
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Session>> m_session;
|
||||
ServerWorld& server_world();
|
||||
|
||||
private:
|
||||
asio::io_context m_io;
|
||||
@@ -26,6 +25,7 @@ private:
|
||||
std::atomic<bool> m_stopped{false};
|
||||
ServerWorld m_world;
|
||||
std::mutex m_session_mutex;
|
||||
std::unordered_map<std::string, std::shared_ptr<Session>> m_session;
|
||||
asio::awaitable<void> listen();
|
||||
void net_run();
|
||||
};
|
||||
|
||||
@@ -115,9 +115,9 @@ private:
|
||||
std::atomic<bool> m_tick_running{true};
|
||||
std::atomic<int> m_per_tick_time = DEFAULT_PER_TICK_TIME; // ms
|
||||
|
||||
std::shared_mutex m_chunks_mutex;
|
||||
mutable std::shared_mutex m_chunks_mutex;
|
||||
std::shared_mutex m_new_chunk_mutex;
|
||||
std::shared_mutex m_player_mutex;
|
||||
mutable std::shared_mutex m_player_mutex;
|
||||
std::mutex m_need_gen_queue_mutex;
|
||||
std::condition_variable_any m_gen_cv;
|
||||
|
||||
|
||||
@@ -13,7 +13,8 @@ class ServerWorld;
|
||||
class Session : public std::enable_shared_from_this<Session> {
|
||||
|
||||
public:
|
||||
Session(tcp::socket socket, ServerWorld& server_world);
|
||||
Session(tcp::socket socket, ServerWorld& server_world,
|
||||
asio::io_context& io);
|
||||
~Session();
|
||||
void start();
|
||||
void send(Packet packet);
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
#include <glad/glad.h>
|
||||
#include <vector>
|
||||
namespace Cubed {
|
||||
class World;
|
||||
class ClientWorld;
|
||||
struct VertexData {
|
||||
std::vector<Vertex3D> m_vertices;
|
||||
GLuint m_vbo = 0;
|
||||
GLuint m_vao = 0;
|
||||
std::atomic<std::size_t> m_sum{0};
|
||||
World& m_world;
|
||||
VertexData(World& world);
|
||||
ClientWorld& m_world;
|
||||
VertexData(ClientWorld& world);
|
||||
~VertexData();
|
||||
VertexData(const VertexData&) = delete;
|
||||
VertexData(VertexData&&) noexcept;
|
||||
|
||||
@@ -11,13 +11,13 @@ namespace Cubed {
|
||||
|
||||
class Camera;
|
||||
class TextureManager;
|
||||
class World;
|
||||
class ClientWorld;
|
||||
class DevPanel;
|
||||
class Renderer {
|
||||
public:
|
||||
constexpr static int NUM_VAO = 7;
|
||||
|
||||
Renderer(const Camera& camera, World& world,
|
||||
Renderer(const Camera& camera, ClientWorld& world,
|
||||
const TextureManager& texture_manager, DevPanel& dev_panel);
|
||||
~Renderer();
|
||||
void hot_reload();
|
||||
@@ -91,7 +91,7 @@ private:
|
||||
const Camera& m_camera;
|
||||
DevPanel& m_dev_panel;
|
||||
const TextureManager& m_texture_manager;
|
||||
World& m_world;
|
||||
ClientWorld& m_world;
|
||||
|
||||
bool m_discard_tranparent = true;
|
||||
bool m_shader_on = true;
|
||||
|
||||
24
src/app.cpp
24
src/app.cpp
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/debug_collector.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
#include "Cubed/tools/system_info.hpp"
|
||||
@@ -58,10 +57,18 @@ void App::init() {
|
||||
// MapTable::init_map();
|
||||
m_texture_manager.init_texture();
|
||||
Logger::info("Texture Load Success");
|
||||
m_world.init_world();
|
||||
|
||||
m_server.start_server();
|
||||
|
||||
m_client = std::make_shared<NetworkClient>(m_client_world);
|
||||
|
||||
m_client->start("127.0.0.1", 25530);
|
||||
// init will send packet
|
||||
m_client_world.init("test", m_client);
|
||||
|
||||
Logger::info("World Init Success");
|
||||
|
||||
m_camera.camera_init(&m_world.get_player("TestPlayer"));
|
||||
m_camera.camera_init(&m_client_world.get_player());
|
||||
m_dev_panel.init();
|
||||
}
|
||||
|
||||
@@ -111,7 +118,7 @@ void App::key_callback(GLFWwindow* window, int key, int scancode, int action,
|
||||
break;
|
||||
}
|
||||
|
||||
app->m_world.get_player("TestPlayer").update_player_move_state(key, action);
|
||||
app->m_client_world.get_player().update_player_move_state(key, action);
|
||||
}
|
||||
|
||||
void App::mouse_button_callback(GLFWwindow* window, int button, int action,
|
||||
@@ -180,7 +187,7 @@ void App::mouse_scroll_callback(GLFWwindow* window, double xoffset,
|
||||
ImGui_ImplGlfw_ScrollCallback(window, xoffset, yoffset);
|
||||
return;
|
||||
}
|
||||
auto& player = app->m_world.get_player("TestPlayer");
|
||||
auto& player = app->m_client_world.get_player();
|
||||
player.update_scroll(yoffset);
|
||||
}
|
||||
|
||||
@@ -244,9 +251,9 @@ void App::update() {
|
||||
std::format("RSS: {}mb", Tools::get_current_rss() / (1024 * 1024)));
|
||||
}
|
||||
m_texture_manager.update();
|
||||
m_world.update(delta_time);
|
||||
m_client_world.update(delta_time);
|
||||
m_camera.update_move_camera();
|
||||
const auto& player = m_world.get_player("TestPlayer");
|
||||
const auto& player = m_client_world.get_player();
|
||||
if (player_gait != player.get_gait()) {
|
||||
player_gait = player.get_gait();
|
||||
float fov = static_cast<float>(Config::get().get<double>("player.fov"));
|
||||
@@ -288,6 +295,7 @@ DevPanel& App::dev_panel() { return m_dev_panel; }
|
||||
Renderer& App::renderer() { return m_renderer; }
|
||||
TextureManager& App::texture_manager() { return m_texture_manager; }
|
||||
Window& App::window() { return m_window; }
|
||||
World& App::world() { return m_world; }
|
||||
ClientWorld& App::client_world() { return m_client_world; }
|
||||
ServerWorld& App::server_world() { return m_server.server_world(); }
|
||||
|
||||
} // namespace Cubed
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "Cubed/camera.hpp"
|
||||
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/gameplay/client_player.hpp"
|
||||
#include "Cubed/gameplay/client_world.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
@@ -22,7 +22,7 @@ void Camera::update_move_camera() {
|
||||
}
|
||||
}
|
||||
|
||||
void Camera::camera_init(Player* player) {
|
||||
void Camera::camera_init(ClientPlayer* player) {
|
||||
m_player = player;
|
||||
update_move_camera();
|
||||
reset_camera();
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "Cubed/app.hpp"
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/gameplay/cave_path.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/gameplay/client_player.hpp"
|
||||
#include "Cubed/gameplay/river.path.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
@@ -59,7 +59,7 @@ static int filter_unsigned(ImGuiInputTextCallbackData* data) {
|
||||
DevPanel::DevPanel(App& app) : m_app(app) {}
|
||||
|
||||
void DevPanel::init() {
|
||||
m_player = &m_app.world().get_player("TestPlayer");
|
||||
m_player = &m_app.client_world().get_player();
|
||||
update_config_view();
|
||||
update_player_profile();
|
||||
}
|
||||
@@ -269,7 +269,7 @@ void DevPanel::show_biome_table_bar() {
|
||||
}
|
||||
|
||||
void DevPanel::show_time_table_bar() {
|
||||
World& world = m_app.world();
|
||||
ServerWorld& world = m_app.server_world();
|
||||
ImGui::Text("Game Tick %llu", world.game_tick());
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("Day Tick %llu", world.day_tick());
|
||||
@@ -342,9 +342,9 @@ void DevPanel::show_river_table_bar() {
|
||||
}
|
||||
|
||||
void DevPanel::show_chunk_table_bar() {
|
||||
auto& world = m_app.world();
|
||||
auto& player = world.get_player("TestPlayer");
|
||||
auto info = world.get_chunk_info(player.get_player_pos());
|
||||
/*
|
||||
auto& world = m_app.client_world();
|
||||
auto& player = world.get_player();
|
||||
|
||||
ImGui::Text("Chunk X: %d Z: %d Info", info.pos.x, info.pos.z);
|
||||
ImGui::Text("Seed: %u", info.seed);
|
||||
@@ -352,7 +352,7 @@ void DevPanel::show_chunk_table_bar() {
|
||||
ImGui::Text("First Random %u", info.first_random);
|
||||
ImGui::Text("%s",
|
||||
std::format("Has Cave Start {}", info.has_cave_start).c_str());
|
||||
ImGui::Text("%s", std::format("Has Cave {}", info.has_cave).c_str());
|
||||
ImGui::Text("%s", std::format("Has Cave {}", info.has_cave).c_str());*/
|
||||
}
|
||||
|
||||
void DevPanel::show_settings_tab_item() {
|
||||
@@ -385,7 +385,7 @@ void DevPanel::show_settings_tab_item() {
|
||||
128)) {
|
||||
Config::get().set("world.rendering_distance",
|
||||
m_config.rendering_distance);
|
||||
m_app.world().hot_reload();
|
||||
m_app.client_world().hot_reload();
|
||||
}
|
||||
if (ImGui::Checkbox("Fullscreen", &m_config.fullscreen)) {
|
||||
Config::get().set("window.fullscreen", m_config.fullscreen);
|
||||
@@ -467,7 +467,7 @@ void DevPanel::show_world_tab_item() {
|
||||
std::strtoul(perlin_noise_input_buffer, nullptr, 10)));
|
||||
m_text_editing.perlin_seed = false;
|
||||
m_player->set_player_pos({0.0f, 255.0f, 0.0f});
|
||||
m_app.world().rebuild_world();
|
||||
m_app.server_world().rebuild_world();
|
||||
}
|
||||
}
|
||||
if (!m_text_editing.perlin_seed) {
|
||||
@@ -476,21 +476,22 @@ void DevPanel::show_world_tab_item() {
|
||||
m_text_editing.perlin_seed = true;
|
||||
}
|
||||
}
|
||||
static int rendering_distance = m_app.world().rendering_distance();
|
||||
static int rendering_distance =
|
||||
m_app.client_world().rendering_distance();
|
||||
if (ImGui::SliderInt("Render Distance", &rendering_distance, 2, 128)) {
|
||||
m_app.world().rendering_distance(rendering_distance);
|
||||
m_app.client_world().rendering_distance(rendering_distance);
|
||||
}
|
||||
ImGui::Text(
|
||||
"Pool Threads %d Max Support Threads %d Reserved Threads %d",
|
||||
m_app.world().pool_threads(), m_app.world().max_threads(),
|
||||
RESERVED_THREADS);
|
||||
m_app.server_world().pool_threads(),
|
||||
m_app.server_world().max_threads(), RESERVED_THREADS);
|
||||
ImGui::SliderInt("Set Pool Threads", &m_threads, 1,
|
||||
m_app.world().max_threads());
|
||||
m_app.server_world().max_threads());
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Set")) {
|
||||
m_app.world().change_pool_threads(m_threads);
|
||||
m_app.server_world().change_pool_threads(m_threads);
|
||||
}
|
||||
if (m_threads > m_app.world().max_threads() - RESERVED_THREADS) {
|
||||
if (m_threads > m_app.server_world().max_threads() - RESERVED_THREADS) {
|
||||
ImGui::TextColored(
|
||||
ImVec4(1.0f, 1.0f, 0.0f, 1.0f),
|
||||
"Waring: When the threads in the thread pool exceed \n(maximum "
|
||||
@@ -498,17 +499,18 @@ void DevPanel::show_world_tab_item() {
|
||||
}
|
||||
|
||||
static const char* chunk_load_style[] = {"Random", "Center"};
|
||||
m_chunk_style = m_app.world().chunk_load_style();
|
||||
m_chunk_style = m_app.server_world().chunk_load_style();
|
||||
if (ImGui::Combo("ChunkLoadStyle", &m_chunk_style, chunk_load_style,
|
||||
IM_ARRAYSIZE(chunk_load_style))) {
|
||||
m_app.world().set_chunk_load_style(m_chunk_style);
|
||||
m_app.server_world().set_chunk_load_style(m_chunk_style);
|
||||
}
|
||||
if (ImGui::Button("Rebuild World")) {
|
||||
m_app.world().rebuild_world();
|
||||
m_app.server_world().rebuild_world();
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Request Chunk Build")) {
|
||||
m_app.world().need_gen();
|
||||
Logger::warn("This Request Chunk Build button is not finish");
|
||||
m_app.server_world().need_gen(std::nullopt);
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Spawn Point")) {
|
||||
@@ -517,9 +519,9 @@ void DevPanel::show_world_tab_item() {
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Checkbox("Gen Thread", &m_gen_thread_running)) {
|
||||
if (m_gen_thread_running) {
|
||||
m_app.world().start_gen_thread();
|
||||
m_app.server_world().start_gen_thread();
|
||||
} else {
|
||||
m_app.world().stop_gen_thread();
|
||||
m_app.server_world().stop_gen_thread();
|
||||
}
|
||||
}
|
||||
// ImGui::Text("Chunk Build Progress\n");
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
#include "Cubed/gameplay/builders/river_builder.hpp"
|
||||
#include "Cubed/gameplay/builders/snowy_plain_builder.hpp"
|
||||
#include "Cubed/gameplay/cave_path.hpp"
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
#include "Cubed/gameplay/river.path.hpp"
|
||||
#include "Cubed/gameplay/server_chunk.hpp"
|
||||
#include "Cubed/gameplay/server_world.hpp"
|
||||
#include "Cubed/gameplay/tree.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
@@ -555,7 +555,8 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
int nx, int nz) -> BlockType {
|
||||
// Search from topmost y downwards for the first non-zero block
|
||||
for (int y = WORLD_HEIGHT - 1; y >= 0; --y) {
|
||||
int idx = Chunk::index(nx, y,
|
||||
int idx =
|
||||
ServerChunk::index(nx, y,
|
||||
nz); // linear index: y * area + z * size + x
|
||||
if (idx >= 0 && idx < static_cast<int>(blocks.size())) {
|
||||
BlockType neighbor_type = blocks[idx];
|
||||
@@ -574,7 +575,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
BlockType type_self = 0;
|
||||
int top_y = -1;
|
||||
top_y = m_heightmap[x][z];
|
||||
type_self = m_blocks[Chunk::index(x, top_y, z)];
|
||||
type_self = m_blocks[ServerChunk::index(x, top_y, z)];
|
||||
|
||||
if (top_y == -1)
|
||||
continue; // no block? skip
|
||||
@@ -665,7 +666,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
if (final_type != type_self) {
|
||||
// top block
|
||||
BlockType new_surface = final_type;
|
||||
m_blocks[Chunk::index(x, top_y, z)] = new_surface;
|
||||
m_blocks[ServerChunk::index(x, top_y, z)] = new_surface;
|
||||
// bottom block
|
||||
unsigned fill_type = 2;
|
||||
if (final_type == 1 || final_type == 8) {
|
||||
@@ -674,7 +675,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
fill_type = final_type;
|
||||
}
|
||||
for (int y = std::max(0, top_y - 5); y < top_y; y++) {
|
||||
m_blocks[Chunk::index(x, y, z)] = fill_type;
|
||||
m_blocks[ServerChunk::index(x, y, z)] = fill_type;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -741,12 +742,12 @@ void ChunkGenerator::generate_cave() {
|
||||
|
||||
carve_worm(path.points(), chunk_pos,
|
||||
[&](int x, int y, int z) -> void {
|
||||
int idx = Chunk::index(x, y, z);
|
||||
int idx = ServerChunk::index(x, y, z);
|
||||
m_chunk.has_cave() = true;
|
||||
if (blocks[idx] == 7)
|
||||
return;
|
||||
if (y < WORLD_SIZE_Y - 1 &&
|
||||
blocks[Chunk::index(x, y + 1, z)] == 7)
|
||||
blocks[ServerChunk::index(x, y + 1, z)] == 7)
|
||||
return;
|
||||
blocks[idx] = 0;
|
||||
});
|
||||
@@ -780,7 +781,7 @@ void ChunkGenerator::generate_river() {
|
||||
|
||||
carve_worm(path.points(), chunk_pos,
|
||||
[&](int x, int y, int z) -> void {
|
||||
int idx = Chunk::index(x, y, z);
|
||||
int idx = ServerChunk::index(x, y, z);
|
||||
if (y > SEA_LEVEL) {
|
||||
blocks[idx] = 0;
|
||||
return;
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
#include "Cubed/gameplay/client_world.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
ClientPlayer::ClientPlayer(ClientWorld& world, std::string_view name)
|
||||
: m_name(name), m_world(world) {}
|
||||
ClientPlayer::ClientPlayer(ClientWorld& world) : m_world(world) {}
|
||||
ClientPlayer::~ClientPlayer() {}
|
||||
|
||||
AABB ClientPlayer::get_aabb() const {
|
||||
@@ -543,5 +542,5 @@ const ClientWorld& ClientPlayer::get_world() const { return m_world; }
|
||||
void ClientPlayer::set_uuid(std::string_view uuid) { m_uuid = uuid; }
|
||||
const std::string& ClientPlayer::get_uuid() const { return m_uuid; }
|
||||
const std::string& ClientPlayer::get_name() const { return m_name; }
|
||||
|
||||
void ClientPlayer::init(std::string_view name) { m_name = name; }
|
||||
} // namespace Cubed
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "Cubed/gameplay/client_world.hpp"
|
||||
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/gameplay/game_time.hpp"
|
||||
#include "Cubed/gameplay/packet.hpp"
|
||||
using namespace std::chrono;
|
||||
@@ -13,9 +14,7 @@ struct ChunkRenderData {
|
||||
};
|
||||
} // namespace
|
||||
|
||||
ClientWorld::ClientWorld(std::string_view player_name,
|
||||
std::shared_ptr<NetworkClient> client)
|
||||
: m_player(*this, player_name), m_client(client) {}
|
||||
ClientWorld::ClientWorld() : m_player(*this) {}
|
||||
|
||||
ClientWorld::~ClientWorld() {
|
||||
stop_client_thread();
|
||||
@@ -186,9 +185,11 @@ void ClientWorld::receive_block_change(const BlockChangeRsp& rsp) {
|
||||
set_block(pos, rsp.block());
|
||||
}
|
||||
|
||||
void ClientWorld::init() {
|
||||
void ClientWorld::init(std::string_view player_name,
|
||||
std::shared_ptr<NetworkClient> client) {
|
||||
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4);
|
||||
|
||||
m_player.init(player_name);
|
||||
m_client = client;
|
||||
// timer
|
||||
register_timer("player_pos", 2, [this]() { report_player_pos(); });
|
||||
|
||||
@@ -219,6 +220,12 @@ void ClientWorld::stop_client_thread() {
|
||||
m_game_running = false;
|
||||
}
|
||||
|
||||
void ClientWorld::hot_reload() {
|
||||
auto& config = Config::get();
|
||||
int dist = config.get<int>("world.rendering_distance");
|
||||
m_rendering_distance = dist <= MAX_DISTANCE ? dist : MAX_DISTANCE;
|
||||
}
|
||||
|
||||
void ClientWorld::client_run(std::stop_token stoken) {
|
||||
Logger::info("Client Thread Started");
|
||||
while (!stoken.stop_requested()) {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "Cubed/tools/log.hpp"
|
||||
namespace Cubed {
|
||||
NetworkClient::NetworkClient(ClientWorld& world)
|
||||
: m_socket(m_io), m_world(world) {}
|
||||
: m_socket(m_io), m_strand(asio::make_strand(m_io)), m_world(world) {}
|
||||
|
||||
NetworkClient::~NetworkClient() { close(); }
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ asio::awaitable<void> NetworkServer::listen() {
|
||||
co_await acceptor.async_accept(asio::use_awaitable);
|
||||
|
||||
std::shared_ptr<Session> s =
|
||||
std::make_shared<Session>(std::move(socket), m_world);
|
||||
std::make_shared<Session>(std::move(socket), m_world, m_io);
|
||||
{
|
||||
std::lock_guard lock(m_session_mutex);
|
||||
m_session.emplace(s->uuid(), s);
|
||||
@@ -83,5 +83,5 @@ void NetworkServer::start_server() {
|
||||
}
|
||||
|
||||
int NetworkServer::port() const { return m_port; }
|
||||
|
||||
ServerWorld& NetworkServer::server_world() { return m_world; }
|
||||
} // namespace Cubed
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "Cubed/gameplay/chunk.hpp"
|
||||
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/debug_collector.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/cubed_hash.hpp"
|
||||
|
||||
@@ -6,9 +6,10 @@
|
||||
using asio::ip::tcp;
|
||||
|
||||
namespace Cubed {
|
||||
Session::Session(tcp::socket socket, ServerWorld& server_world)
|
||||
: m_socket(std::move(socket)), m_uuid(generate_uuid()),
|
||||
m_server_world(server_world) {}
|
||||
Session::Session(tcp::socket socket, ServerWorld& server_world,
|
||||
asio::io_context& io)
|
||||
: m_socket(std::move(socket)), m_strand(asio::make_strand(io)),
|
||||
m_uuid(generate_uuid()), m_server_world(server_world) {}
|
||||
|
||||
Session::~Session() {}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include "Cubed/gameplay/vertex_data.hpp"
|
||||
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/gameplay/client_world.hpp"
|
||||
|
||||
namespace Cubed {
|
||||
VertexData::VertexData(World& world) : m_world(world) {}
|
||||
VertexData::VertexData(ClientWorld& world) : m_world(world) {}
|
||||
VertexData::~VertexData() {
|
||||
if (m_vbo != 0) {
|
||||
m_world.push_delete_vbo(m_vbo);
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/debug_collector.hpp"
|
||||
#include "Cubed/dev_panel.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/gameplay/world.hpp"
|
||||
#include "Cubed/gameplay/client_player.hpp"
|
||||
#include "Cubed/gameplay/client_world.hpp"
|
||||
#include "Cubed/primitive_data.hpp"
|
||||
#include "Cubed/texture_manager.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
Renderer::Renderer(const Camera& camera, World& world,
|
||||
Renderer::Renderer(const Camera& camera, ClientWorld& world,
|
||||
const TextureManager& texture_manager, DevPanel& dev_panel)
|
||||
: m_camera(camera), m_dev_panel(dev_panel),
|
||||
m_texture_manager(texture_manager), m_world(world) {}
|
||||
|
||||
Reference in New Issue
Block a user