Ejemplo n.º 1
0
        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.");
                }
            }
        }