Fixed camera data not being sent when coordinates are too large

This commit is contained in:
Yousef Khadadeh 2025-11-30 11:47:57 +03:00
parent 0fb1f15cca
commit 470faf1cc5
6 changed files with 58 additions and 21 deletions

View file

@ -14,7 +14,7 @@ template <class T> std::weak_ptr<T> make_weak_ptr(std::shared_ptr<T> ptr) { retu
#define RELIABLE_COMMAND_CHANNEL "reliableCommand"
#define UNRELIABLE_COMMAND_CHANNEL "unreliableCommand"
#define RESOURCE_COMMAND_CHANNEL "resourceCommand"
#define MAIN_NETLIBRARY_CHANNEL "main00004" // NOTE: Change this value whenever netcode is incompatible with the previous version
#define MAIN_NETLIBRARY_CHANNEL "main00005" // NOTE: Change this value whenever netcode is incompatible with the previous version
typedef uint8_t MessageCommandType;

View file

@ -11,10 +11,10 @@ class MainServer;
class ClientData {
public:
CoordSpaceHelper camCoords;
Vector2f windowSize;
Vector2f cursorPos;
Vector2f windowSize = {1000.0f, 1000.0f};
Vector2f cursorPos = {1.0f, 1.0f};
Vector3f cursorColor;
Vector3f cursorColor = {1.0f, 1.0f, 1.0f};
std::string displayName;
ServerPortionID serverID = 0;

View file

@ -6,7 +6,8 @@ enum ServerCommands : MessageCommandType {
SERVER_FRAGMENT = 0,
SERVER_KEEP_ALIVE,
SERVER_INITIAL_DATA,
SERVER_MOVE_MOUSE,
SERVER_MOVE_CAMERA,
SERVER_MOVE_SCREEN_MOUSE,
SERVER_PLACE_SINGLE_COMPONENT,
SERVER_PLACE_MANY_COMPONENTS,
SERVER_TRANSFORM_MANY_COMPONENTS,
@ -36,7 +37,8 @@ enum ClientCommands : MessageCommandType {
CLIENT_ERASE_SINGLE_COMPONENT,
CLIENT_ERASE_MANY_COMPONENTS,
CLIENT_UPDATE_COMPONENT,
CLIENT_MOVE_MOUSE,
CLIENT_MOVE_CAMERA,
CLIENT_MOVE_SCREEN_MOUSE,
CLIENT_NEW_RESOURCE_ID,
CLIENT_NEW_RESOURCE_DATA,
CLIENT_CHAT_MESSAGE,

View file

@ -53,8 +53,15 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
message(newClient.displayName, isDirectConnect);
ensure_display_name_unique(newClient.displayName);
if(isDirectConnect)
if(isDirectConnect) {
fileDisplayName = newClient.displayName;
message(newClient.camCoords, newClient.windowSize);
}
else {
auto& [randomClientServerID, randomClient] = *clients.begin();
newClient.camCoords = randomClient.camCoords;
newClient.windowSize = randomClient.windowSize;
}
newClient.serverID = Random::get().int_range<ServerPortionID>(1, std::numeric_limits<ServerPortionID>::max());
client->customID = newClient.serverID;
@ -62,7 +69,7 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
if(isDirectConnect)
netServer->send_items_to_client(client, RELIABLE_COMMAND_CHANNEL, CLIENT_INITIAL_DATA, isDirectConnect, newClient.serverID, newClient.cursorColor);
else {
netServer->send_items_to_client(client, RELIABLE_COMMAND_CHANNEL, CLIENT_INITIAL_DATA, isDirectConnect, newClient.serverID, newClient.cursorColor, newClient.displayName, fileDisplayName, clients, data);
netServer->send_items_to_client(client, RELIABLE_COMMAND_CHANNEL, CLIENT_INITIAL_DATA, isDirectConnect, newClient.serverID, newClient.cursorColor, newClient.displayName, newClient.camCoords, newClient.windowSize, fileDisplayName, clients, data);
for(auto& [id, rData] : data.resources) {
netServer->send_items_to_client(client, RESOURCE_COMMAND_CHANNEL, CLIENT_NEW_RESOURCE_ID, id);
netServer->send_items_to_client(client, RESOURCE_COMMAND_CHANNEL, CLIENT_NEW_RESOURCE_DATA, rData);
@ -70,10 +77,15 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
}
clients.emplace(newClient.serverID, newClient);
});
netServer->add_recv_callback(SERVER_MOVE_MOUSE, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
netServer->add_recv_callback(SERVER_MOVE_CAMERA, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
auto& c = clients[client->customID];
message(c.camCoords, c.windowSize, c.cursorPos);
netServer->send_items_to_all_clients_except(client, UNRELIABLE_COMMAND_CHANNEL, CLIENT_MOVE_MOUSE, c.serverID, c.camCoords, c.windowSize, c.cursorPos);
message(c.camCoords, c.windowSize);
netServer->send_items_to_all_clients_except(client, RELIABLE_COMMAND_CHANNEL, CLIENT_MOVE_CAMERA, c.serverID, c.camCoords, c.windowSize);
});
netServer->add_recv_callback(SERVER_MOVE_SCREEN_MOUSE, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
auto& c = clients[client->customID];
message(c.cursorPos);
netServer->send_items_to_all_clients_except(client, UNRELIABLE_COMMAND_CHANNEL, CLIENT_MOVE_SCREEN_MOUSE, c.serverID, c.cursorPos);
});
netServer->add_recv_callback(SERVER_PLACE_SINGLE_COMPONENT, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
uint64_t placement;

View file

@ -76,18 +76,17 @@ void World::init_client_callbacks() {
message(isDirectConnect, ownID, userColor);
if(!isDirectConnect) {
std::string fileDisplayName;
message(displayName, fileDisplayName, clients);
CoordSpaceHelper camCoordsToMoveTo;
Vector2f windowSizeToMoveTo;
message(displayName, camCoordsToMoveTo, windowSizeToMoveTo, fileDisplayName, clients);
set_name(fileDisplayName);
drawData.cam.smooth_move_to(*main.world, camCoordsToMoveTo, windowSizeToMoveTo, true);
Vector3f newBackColor;
message(newBackColor);
set_canvas_background_color(newBackColor, false);
message(canvasScale);
drawProg.initialize_draw_data(message);
message(bMan, gridMan);
if(!clients.empty()) {
auto& [randomClientServerID, randomClient] = *clients.begin();
drawData.cam.smooth_move_to(*main.world, randomClient.camCoords, randomClient.windowSize, true);
}
}
nextClientID = get_max_id(ownID);
clientStillConnecting = false;
@ -110,11 +109,17 @@ void World::init_client_callbacks() {
clients.erase(id);
}
});
con.client_add_recv_callback(CLIENT_MOVE_MOUSE, [&](cereal::PortableBinaryInputArchive& message) {
con.client_add_recv_callback(CLIENT_MOVE_CAMERA, [&](cereal::PortableBinaryInputArchive& message) {
ServerPortionID id;
message(id);
auto& c = clients[id];
message(c.camCoords, c.windowSize, c.cursorPos);
message(c.camCoords, c.windowSize);
});
con.client_add_recv_callback(CLIENT_MOVE_SCREEN_MOUSE, [&](cereal::PortableBinaryInputArchive& message) {
ServerPortionID id;
message(id);
auto& c = clients[id];
message(c.cursorPos);
});
con.client_add_recv_callback(CLIENT_CHAT_MESSAGE, [&](cereal::PortableBinaryInputArchive& message) {
ServerPortionID senderID;
@ -164,7 +169,19 @@ void World::focus_update() {
}
if(!clientStillConnecting) {
con.client_send_items_to_server(UNRELIABLE_COMMAND_CHANNEL, SERVER_MOVE_MOUSE, drawData.cam.c, main.window.size.cast<float>().eval(), main.input.mouse.pos);
constexpr float SECONDS_TO_SEND_CAMERA_DATA = 0.5f;
timeToSendCameraData.update_time_since();
if(timeToSendCameraData.get_time_since() > SECONDS_TO_SEND_CAMERA_DATA) {
timeToSendCameraData.update_time_point();
if(!sentCameraData.has_value() || (sentCameraData.value().windowSize != main.window.size.cast<float>().eval() || sentCameraData.value().camTransform != drawData.cam.c)) {
con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_MOVE_CAMERA, drawData.cam.c, main.window.size.cast<float>().eval());
sentCameraData = {
.camTransform = drawData.cam.c,
.windowSize = main.window.size.cast<float>()
};
}
}
con.client_send_items_to_server(UNRELIABLE_COMMAND_CHANNEL, SERVER_MOVE_SCREEN_MOUSE, main.input.mouse.pos);
drawProg.update();
}
@ -263,7 +280,7 @@ void World::start_hosting(const std::string& initNetSource, const std::string& s
bMan.init_client_callbacks();
gridMan.init_client_callbacks();
init_client_callbacks();
con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_INITIAL_DATA, displayName, true);
con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_INITIAL_DATA, displayName, true, drawData.cam.c, main.window.size.cast<float>().eval());
conType = CONNECTIONTYPE_SERVER;
netSource = initNetSource;
drawProg.components.set_server_from_client_list();

View file

@ -100,7 +100,6 @@ class World {
uint64_t canvasScale = 0;
private:
ClientPortionID get_max_id(ServerPortionID serverID);
void init_client_callbacks();
@ -108,6 +107,13 @@ class World {
void draw_other_player_cursors(SkCanvas* canvas, const DrawData& drawData);
TimePoint timeToSendCameraData;
struct SentCameraData {
CoordSpaceHelper camTransform;
Vector2f windowSize;
};
std::optional<SentCameraData> sentCameraData;
ClientPortionID nextClientID = 0;
WorldVec mousePreviousWorldVec = {0, 0};
WorldVec mouseWorldMove = {0, 0};