public static void SendMapToServer() { ZLog.Log("-------------------- SENDING VPLUSMAPSYNC DATA"); //Convert exploration data to ranges List <MapRange> exploredAreas = ExplorationDataToMapRanges(Minimap.instance.m_explored); //If we have no data to send, just send an empty RPC to trigger the server end to sync. if (exploredAreas.Count == 0) { ZPackage pkg = new ZPackage(); pkg.Write(0); //Number of explored areas we're sending (zero in this case) pkg.Write(true); //Trigger server sync by telling the server this is the last package we'll be sending. ZRoutedRpc.instance.InvokeRoutedRPC(ZRoutedRpc.instance.GetServerPeerID(), "VPlusMapSync", new object[] { pkg }); } else //We have data to send. Prep it and send it. { //Chunk map data List <ZPackage> packages = ChunkMapData(exploredAreas); //Route all chunks to the server foreach (ZPackage pkg in packages) { RpcQueue.Enqueue(new RpcData() { Name = "VPlusMapSync", Payload = new object[] { pkg }, Target = ZRoutedRpc.instance.GetServerPeerID() }); } ZLog.Log($"Sent my map data to the server ({exploredAreas.Count} map ranges, {packages.Count} chunks)"); } }
public static void RPC_VPlusMapSync(long sender, ZPackage mapPkg) { if (ZNet.m_isServer) //Server { if (sender == ZRoutedRpc.instance.GetServerPeerID()) { return; } if (mapPkg == null) { return; } //Get number of explored areas int exploredAreaCount = mapPkg.ReadInt(); if (exploredAreaCount > 0) { //Iterate and add them to server's combined map data. for (int i = 0; i < exploredAreaCount; i++) { MapRange exploredArea = mapPkg.ReadVPlusMapRange(); for (int x = exploredArea.StartingX; x < exploredArea.EndingX; x++) { ServerMapData[exploredArea.Y * Minimap.instance.m_textureSize + x] = true; } } ZLog.Log($"Received {exploredAreaCount} map ranges from peer #{sender}."); //Send Ack VPlusAck.SendAck(sender); } //Check if this is the last chunk from the client. bool lastMapPackage = mapPkg.ReadBool(); if (!lastMapPackage) { return; //This package is one of many chunks, so don't update clients until we get all of them. } //Convert map data into ranges List <MapRange> serverExploredAreas = ExplorationDataToMapRanges(ServerMapData); //Chunk up the map data List <ZPackage> packages = ChunkMapData(serverExploredAreas); //Send the updated server map to all clients foreach (ZPackage pkg in packages) { RpcQueue.Enqueue(new RpcData() { Name = "VPlusMapSync", Payload = new object[] { pkg }, Target = ZRoutedRpc.Everybody }); } ZLog.Log($"-------------------------- Packages: {packages.Count}"); ZLog.Log($"Sent map updates to all clients ({serverExploredAreas.Count} map ranges, {packages.Count} chunks)"); } else //Client { if (sender != ZRoutedRpc.instance.GetServerPeerID()) { return; //Only bother if it's from the server. } if (mapPkg == null) { ZLog.LogWarning("Warning: Got empty map sync package from server."); return; } //Get number of explored areas int exploredAreaCount = mapPkg.ReadInt(); if (exploredAreaCount > 0) { //Iterate and add them to explored map for (int i = 0; i < exploredAreaCount; i++) { MapRange exploredArea = mapPkg.ReadVPlusMapRange(); for (int x = exploredArea.StartingX; x < exploredArea.EndingX; x++) { Minimap.instance.Explore(x, exploredArea.Y); } } //Update fog texture Minimap.instance.m_fogTexture.Apply(); ZLog.Log($"I got {exploredAreaCount} map ranges from the server!"); //Send Ack VPlusAck.SendAck(sender); } else { ZLog.Log("Server has no explored areas to sync, continuing."); } } }