From c4eb7e944d343110bf53533431cf12318fdbc401 Mon Sep 17 00:00:00 2001 From: devbeni Date: Sun, 14 Dec 2025 21:00:15 +0100 Subject: [PATCH] Fix map synchronization between host and clients MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: Clients received WorldSeed packet before ServerSettings, causing world generation with wrong mapSize/mapBias/mapRivers parameters. Same seed but different parameters = different maps. Solution: Include all map parameters directly in WorldSeed packet: - WorldSize (map size) - WorldType (map bias - continents/islands/etc) - WorldRivers (river/lake density) Now packet order doesn't matter - WorldSeed has everything needed for identical world generation across all clients. Changes: - WorldSeed.cs: Add map parameters, set before Generate() - ClientConnected.cs: Send full world params to joining clients - ServerLobbyScript.cs: Send full params on new world generation - Added [WORLD SYNC] debug logging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- Enums/Packets.cs | 3 --- Packets/Lobby/WorldSeed.cs | 14 ++++++++++++++ Packets/Network/ClientConnected.cs | 9 +++++++-- ServerLobby/ServerLobbyScript.cs | 8 +++++++- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Enums/Packets.cs b/Enums/Packets.cs index f7da401..57625c4 100644 --- a/Enums/Packets.cs +++ b/Enums/Packets.cs @@ -18,11 +18,8 @@ namespace KCM.Enums KingdomName = 32, StartGame = 33, WorldSeed = 34, - - Building = 50, BuildingOnPlacement = 51, - World = 70, WorldPlace = 71, FellTree = 72, diff --git a/Packets/Lobby/WorldSeed.cs b/Packets/Lobby/WorldSeed.cs index 38655c9..a276063 100644 --- a/Packets/Lobby/WorldSeed.cs +++ b/Packets/Lobby/WorldSeed.cs @@ -13,6 +13,11 @@ namespace KCM.Packets.Lobby public override ushort packetId => (int)Enums.Packets.WorldSeed; public int Seed { get; set; } + // Map parameters to ensure correct generation + public World.MapSize WorldSize { get; set; } + public World.MapBias WorldType { get; set; } + public World.MapRiverLakes WorldRivers { get; set; } + public override void HandlePacketServer() { //SetWorldSeed(); @@ -27,12 +32,21 @@ namespace KCM.Packets.Lobby { try { + // Set map parameters BEFORE generating to ensure correct world + World.inst.mapSize = WorldSize; + World.inst.mapBias = WorldType; + World.inst.mapRiverLakes = WorldRivers; + + Main.helper.Log($"[WORLD SYNC] Generating world - Seed: {Seed}, Size: {WorldSize}, Type: {WorldType}, Rivers: {WorldRivers}"); + foreach (var player in Main.kCPlayers.Values) player.inst.Reset(); World.inst.Generate(Seed); Vector3 center = World.inst.GetCellData(World.inst.GridWidth / 2, World.inst.GridHeight / 2).Center; Cam.inst.SetTrackingPos(center); + + Main.helper.Log("[WORLD SYNC] World generation complete"); } catch (Exception e) { diff --git a/Packets/Network/ClientConnected.cs b/Packets/Network/ClientConnected.cs index 6c55639..0e89e3b 100644 --- a/Packets/Network/ClientConnected.cs +++ b/Packets/Network/ClientConnected.cs @@ -111,11 +111,16 @@ namespace KCM.Packets.Network } else { - + // Send world seed with all map parameters to ensure correct generation new WorldSeed() { - Seed = World.inst.seed + Seed = World.inst.seed, + WorldSize = World.inst.mapSize, + WorldType = World.inst.mapBias, + WorldRivers = World.inst.mapRiverLakes }.SendToAll(KCClient.client.Id); + + Main.helper.Log($"[WORLD SYNC] Sent world to new client - Seed: {World.inst.seed}, Size: {World.inst.mapSize}, Type: {World.inst.mapBias}, Rivers: {World.inst.mapRiverLakes}"); } } diff --git a/ServerLobby/ServerLobbyScript.cs b/ServerLobby/ServerLobbyScript.cs index a4990fa..3917fa3 100644 --- a/ServerLobby/ServerLobbyScript.cs +++ b/ServerLobby/ServerLobbyScript.cs @@ -247,10 +247,16 @@ namespace KCM foreach (var player in Main.kCPlayers.Values) player.inst.SetupJobPriorities(); + // Send world seed with all map parameters to ensure clients generate identical world new WorldSeed() { - Seed = World.inst.seed + Seed = World.inst.seed, + WorldSize = World.inst.mapSize, + WorldType = World.inst.mapBias, + WorldRivers = World.inst.mapRiverLakes }.SendToAll(KCClient.client.Id); + + Main.helper.Log($"[WORLD SYNC] Generated new world - Seed: {World.inst.seed}, Size: {World.inst.mapSize}, Type: {World.inst.mapBias}, Rivers: {World.inst.mapRiverLakes}"); } catch (Exception ex) {