Beispiel #1
0
 public void SendChunk(Server server, int clientid, Vector3i globalpos, Vector3i chunkpos)
 {
     ClientOnServer c = server.clients[clientid];
     ServerChunk chunk = server.d_Map.GetChunk(globalpos.x, globalpos.y, globalpos.z);
     server.ClientSeenChunkSet(clientid, chunkpos.x, chunkpos.y, chunkpos.z, (int)server.simulationcurrentframe);
     //sent++;
     byte[] compressedchunk;
     if (MapUtil.IsSolidChunk(chunk.data) && chunk.data[0] == 0)
     {
         //don't send empty chunk.
         compressedchunk = null;
     }
     else
     {
         compressedchunk = server.CompressChunkNetwork(chunk.data);
         //todo!
         //commented because it was being sent too early, before full column was generated.
         //if (!c.heightmapchunksseen.ContainsKey(new Vector2i(v.x, v.y)))
         {
             byte[] heightmapchunk = Misc.UshortArrayToByteArray(server.d_Map.GetHeightmapChunk(globalpos.x, globalpos.y));
             byte[] compressedHeightmapChunk = server.d_NetworkCompression.Compress(heightmapchunk);
             Packet_ServerHeightmapChunk p1 = new Packet_ServerHeightmapChunk()
             {
                 X = globalpos.x,
                 Y = globalpos.y,
                 SizeX = Server.chunksize,
                 SizeY = Server.chunksize,
                 CompressedHeightmap = compressedHeightmapChunk,
             };
             server.SendPacket(clientid, server.Serialize(new Packet_Server() { Id = Packet_ServerIdEnum.HeightmapChunk, HeightmapChunk = p1 }));
             c.heightmapchunksseen[new Vector2i(globalpos.x, globalpos.y)] = (int)server.simulationcurrentframe;
         }
     }
     if (compressedchunk != null)
     {
         foreach (byte[] part in Server.Parts(compressedchunk, 1024))
         {
             Packet_ServerChunkPart p1 = new Packet_ServerChunkPart()
             {
                 CompressedChunkPart = part,
             };
             server.SendPacket(clientid, server.Serialize(new Packet_Server() { Id = Packet_ServerIdEnum.ChunkPart, ChunkPart = p1 }));
         }
     }
     Packet_ServerChunk p = new Packet_ServerChunk()
     {
         X = globalpos.x,
         Y = globalpos.y,
         Z = globalpos.z,
         SizeX = Server.chunksize,
         SizeY = Server.chunksize,
         SizeZ = Server.chunksize,
     };
     server.SendPacket(clientid, server.Serialize(new Packet_Server() { Id = Packet_ServerIdEnum.Chunk_, Chunk_ = p }));
 }
    void ProcessInBackground(Packet_Server packet)
    {
        switch (packet.Id)
        {
        case Packet_ServerIdEnum.ChunkPart:
            byte[] arr       = packet.ChunkPart.CompressedChunkPart;
            int    arrLength = game.platform.ByteArrayLength(arr);  // todo
            for (int i = 0; i < arrLength; i++)
            {
                CurrentChunk[CurrentChunkCount++] = arr[i];
            }
            break;

        case Packet_ServerIdEnum.Chunk_:
        {
            Packet_ServerChunk p = packet.Chunk_;
            if (CurrentChunkCount != 0)
            {
                game.platform.GzipDecompress(CurrentChunk, CurrentChunkCount, decompressedchunk);
                {
                    int i = 0;
                    for (int zz = 0; zz < p.SizeZ; zz++)
                    {
                        for (int yy = 0; yy < p.SizeY; yy++)
                        {
                            for (int xx = 0; xx < p.SizeX; xx++)
                            {
                                int block = (decompressedchunk[i + 1] << 8) + decompressedchunk[i];
                                if (block < GlobalVar.MAX_BLOCKTYPES)
                                {
                                    receivedchunk[Index3d(xx, yy, zz, p.SizeX, p.SizeY)] = block;
                                }
                                i += 2;
                            }
                        }
                    }
                }
            }
            else
            {
                int size = p.SizeX * p.SizeY * p.SizeZ;
                for (int i = 0; i < size; i++)
                {
                    receivedchunk[i] = 0;
                }
            }
            {
                game.map.SetMapPortion(p.X, p.Y, p.Z, receivedchunk, p.SizeX, p.SizeY, p.SizeZ);
                for (int xx = 0; xx < 2; xx++)
                {
                    for (int yy = 0; yy < 2; yy++)
                    {
                        for (int zz = 0; zz < 2; zz++)
                        {
                            //d_Shadows.OnSetChunk(p.X + 16 * xx, p.Y + 16 * yy, p.Z + 16 * zz);//todo
                        }
                    }
                }
            }
            game.ReceivedMapLength += CurrentChunkCount;        // lengthPrefixLength + packetLength;
            CurrentChunkCount       = 0;
        }
        break;

        case Packet_ServerIdEnum.HeightmapChunk:
        {
            Packet_ServerHeightmapChunk p = packet.HeightmapChunk;
            game.platform.GzipDecompress(p.CompressedHeightmap, game.platform.ByteArrayLength(p.CompressedHeightmap), decompressedchunk);
            int[] decompressedchunk1 = Game.ByteArrayToUshortArray(decompressedchunk, p.SizeX * p.SizeY * 2);
            for (int xx = 0; xx < p.SizeX; xx++)
            {
                for (int yy = 0; yy < p.SizeY; yy++)
                {
                    int height = decompressedchunk1[MapUtilCi.Index2d(xx, yy, p.SizeX)];
                    game.d_Heightmap.SetBlock(p.X + xx, p.Y + yy, height);
                }
            }
        }
        break;
        }
    }
Beispiel #3
0
        public void SendChunk(Server server, int clientid, Vector3i globalpos, Vector3i chunkpos)
        {
            ClientOnServer c     = server.clients[clientid];
            ServerChunk    chunk = server.d_Map.GetChunk(globalpos.x, globalpos.y, globalpos.z);

            server.ClientSeenChunkSet(clientid, chunkpos.x, chunkpos.y, chunkpos.z, (int)server.simulationcurrentframe);
            //sent++;
            byte[] compressedchunk;
            if (MapUtil.IsSolidChunk(chunk.data) && chunk.data[0] == 0)
            {
                //don't send empty chunk.
                compressedchunk = null;
            }
            else
            {
                compressedchunk = server.CompressChunkNetwork(chunk.data);
                //TODO: commented because it was being sent too early, before full column was generated.
                //if (!c.heightmapchunksseen.ContainsKey(new Vector2i(v.x, v.y)))
                {
                    byte[] heightmapchunk           = Misc.UshortArrayToByteArray(server.d_Map.GetHeightmapChunk(globalpos.x, globalpos.y));
                    byte[] compressedHeightmapChunk = server.d_NetworkCompression.Compress(heightmapchunk);
                    Packet_ServerHeightmapChunk p1  = new Packet_ServerHeightmapChunk()
                    {
                        X     = globalpos.x,
                        Y     = globalpos.y,
                        SizeX = Server.chunksize,
                        SizeY = Server.chunksize,
                        CompressedHeightmap = compressedHeightmapChunk,
                    };
                    server.SendPacket(clientid, server.Serialize(new Packet_Server()
                    {
                        Id             = Packet_ServerIdEnum.HeightmapChunk,
                        HeightmapChunk = p1
                    }));
                    c.heightmapchunksseen[new Vector2i(globalpos.x, globalpos.y)] = (int)server.simulationcurrentframe;
                }
            }
            if (compressedchunk != null)
            {
                foreach (byte[] part in Server.Parts(compressedchunk, 1024))
                {
                    Packet_ServerChunkPart p1 = new Packet_ServerChunkPart()
                    {
                        CompressedChunkPart = part,
                    };
                    server.SendPacket(clientid, server.Serialize(new Packet_Server()
                    {
                        Id        = Packet_ServerIdEnum.ChunkPart,
                        ChunkPart = p1
                    }));
                }
            }
            Packet_ServerChunk p = new Packet_ServerChunk()
            {
                X     = globalpos.x,
                Y     = globalpos.y,
                Z     = globalpos.z,
                SizeX = Server.chunksize,
                SizeY = Server.chunksize,
                SizeZ = Server.chunksize,
            };

            server.SendPacket(clientid, server.Serialize(new Packet_Server()
            {
                Id     = Packet_ServerIdEnum.Chunk_,
                Chunk_ = p
            }));
        }