Exemple #1
0
    void LoadAndSendChunk(int x, int y, int z)
    {
        ClientSimple c   = server.clients[clientId];
        int          pos = MapUtilCi.Index2d(x, y, server.MapSizeX / ServerSimple.ChunkSize);

        if (c.chunksseen[pos] == null)
        {
            c.chunksseen[pos] = new bool[server.MapSizeZ / ServerSimple.ChunkSize];
        }
        c.chunksseen[pos][z] = true;

        int[] chunk = new int[32 * 32 * 32];

        for (int i = 0; i < server.modsCount; i++)
        {
            server.mods[i].GenerateChunk(x, y, z, chunk);
        }

        byte[] chunkBytes       = MiscCi.UshortArrayToByteArray(chunk, 32 * 32 * 32);
        IntRef compressedLength = new IntRef();

        byte[] chunkCompressed = server.platform.GzipCompress(chunkBytes, 32 * 32 * 32 * 2, compressedLength);

        server.QueueMainThreadAction(SendPacketAction.Create(server, clientId, ServerPackets.ChunkPart(chunkCompressed)));
        server.QueueMainThreadAction(SendPacketAction.Create(server, clientId, ServerPackets.Chunk_(x * ServerSimple.ChunkSize, y * ServerSimple.ChunkSize, z * ServerSimple.ChunkSize, ServerSimple.ChunkSize)));
    }
Exemple #2
0
    static int Texture2d(GamePlatform platform, int[] pixelsArgb, float x, float y)
    {
        int px = platform.FloatToInt(x * (textureSize - 1));
        int py = platform.FloatToInt(y * (textureSize - 1));

        px = positive_modulo(px, (textureSize - 1));
        py = positive_modulo(py, (textureSize - 1));
        return(pixelsArgb[MapUtilCi.Index2d(px, py, textureSize)]);
    }
Exemple #3
0
 static int GetLightHeight(Game game, int cx, int cy, int xx, int yy)
 {
     int[] chunk = game.d_Heightmap.chunks[MapUtilCi.Index2d(cx, cy, game.map.MapSizeX / Game.chunksize)];
     if (chunk == null)
     {
         return(0);
     }
     return(chunk[MapUtilCi.Index2d(xx % Game.chunksize, yy % Game.chunksize, Game.chunksize)]);
 }
Exemple #4
0
 public ServerChunk GetChunkValid(int cx, int cy, int cz)
 {
     ServerChunk[] column = chunks[MapUtilCi.Index2d(cx, cy, MapSizeX / chunksize)];
     if (column == null)
     {
         return(null);
     }
     return(column[cz]);
 }
Exemple #5
0
 public void SetChunkValid(int cx, int cy, int cz, ServerChunk chunk)
 {
     ServerChunk[] column = chunks[MapUtilCi.Index2d(cx, cy, MapSizeX / chunksize)];
     if (column == null)
     {
         column = new ServerChunk[MapSizeZ / chunksize];
         chunks[MapUtilCi.Index2d(cx, cy, MapSizeX / chunksize)] = column;
     }
     column[cz] = chunk;
 }
Exemple #6
0
 public void SetChunkValid(int cx, int cy, int cz, ServerChunk chunk)
 {
     unchecked
     {
         ServerChunk[] column = chunks[MapUtilCi.Index2d(cx, cy, invertChunk(MapSizeX))];
         if (column == null)
         {
             column = new ServerChunk[invertChunk(MapSizeZ)];
             chunks[MapUtilCi.Index2d(cx, cy, invertChunk(MapSizeX))] = column;
         }
         column[cz] = chunk;
     }
 }
Exemple #7
0
 public unsafe ushort[] GetHeightmapChunk(int x, int y)
 {
     //TODO: don't copy
     ushort[] chunk2d = d_Heightmap.GetChunk(x, y);
     ushort[] chunk   = new ushort[chunksize * chunksize];
     for (int xx = 0; xx < chunksize; xx++)
     {
         for (int yy = 0; yy < chunksize; yy++)
         {
             chunk[MapUtilCi.Index2d(xx, yy, chunksize)] = chunk2d[MapUtilCi.Index2d(xx, yy, chunksize)];
         }
     }
     //TODO: ushort[]
     return(chunk);
 }
    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;
        }
    }
Exemple #9
0
    void NearestDirty(int clientid, int playerx, int playery, int playerz, int[] retNearest)
    {
        int nearestdist = intMaxValue;

        retNearest[0] = -1;
        retNearest[1] = -1;
        retNearest[2] = -1;
        int px = (playerx) / ServerSimple.ChunkSize;
        int py = (playery) / ServerSimple.ChunkSize;
        int pz = (playerz) / ServerSimple.ChunkSize;

        int chunksxy = this.mapAreaSize() / ServerSimple.ChunkSize / 2;
        int chunksz  = this.mapAreaSizeZ() / ServerSimple.ChunkSize / 2;

        int startx = px - chunksxy;
        int endx   = px + chunksxy;
        int starty = py - chunksxy;
        int endy   = py + chunksxy;
        int startz = pz - chunksz;
        int endz   = pz + chunksz;

        if (startx < 0)
        {
            startx = 0;
        }
        if (starty < 0)
        {
            starty = 0;
        }
        if (startz < 0)
        {
            startz = 0;
        }
        if (endx >= mapsizexchunks())
        {
            endx = mapsizexchunks() - 1;
        }
        if (endy >= mapsizeychunks())
        {
            endy = mapsizeychunks() - 1;
        }
        if (endz >= mapsizezchunks())
        {
            endz = mapsizezchunks() - 1;
        }

        ClientSimple client = server.clients[clientid];

        for (int x = startx; x <= endx; x++)
        {
            for (int y = starty; y <= endy; y++)
            {
                int pos = MapUtilCi.Index2d(x, y, server.MapSizeX / ServerSimple.ChunkSize);
                if (client.chunksseen[pos] == null)
                {
                    client.chunksseen[pos] = new bool[server.MapSizeZ / ServerSimple.ChunkSize];
                }
                for (int z = startz; z <= endz; z++)
                {
                    bool[] column = client.chunksseen[pos];
                    if (column[z])
                    {
                        continue;
                    }
                    {
                        int dx   = px - x;
                        int dy   = py - y;
                        int dz   = pz - z;
                        int dist = dx * dx + dy * dy + dz * dz;
                        if (dist < nearestdist)
                        {
                            nearestdist   = dist;
                            retNearest[0] = x;
                            retNearest[1] = y;
                            retNearest[2] = z;
                        }
                    }
                }
            }
        }
    }
Exemple #10
0
    /// <summary>
    /// Creates a bitmap from a given string
    /// </summary>
    /// <param name="t">The <see cref="Text_"/> object to create an image from</param>
    /// <returns>A <see cref="BitmapCi"/> containing the rendered text</returns>
    internal BitmapCi CreateTextTexture(Text_ t)
    {
        IntRef partsCount = new IntRef();

        TextPart[] parts = DecodeColors(t.text, t.color, partsCount);

        float totalwidth  = 0;
        float totalheight = 0;

        int[] sizesX = new int[partsCount.value];
        int[] sizesY = new int[partsCount.value];

        for (int i = 0; i < partsCount.value; i++)
        {
            IntRef outWidth  = new IntRef();
            IntRef outHeight = new IntRef();
            platform.TextSize(parts[i].text, t.font, outWidth, outHeight);

            sizesX[i] = outWidth.value;
            sizesY[i] = outHeight.value;

            totalwidth += outWidth.value;
            totalheight = MathCi.MaxFloat(totalheight, outHeight.value);
        }

        int      size2X = NextPowerOfTwo(platform.FloatToInt(totalwidth) + 1);
        int      size2Y = NextPowerOfTwo(platform.FloatToInt(totalheight) + 1);
        BitmapCi bmp2   = platform.BitmapCreate(size2X, size2Y);

        int[] bmp2Pixels = new int[size2X * size2Y];

        float currentwidth = 0;

        for (int i = 0; i < partsCount.value; i++)
        {
            int sizeiX = sizesX[i];
            int sizeiY = sizesY[i];
            if (sizeiX == 0 || sizeiY == 0)
            {
                continue;
            }

            Text_ partText = new Text_();
            partText.text  = parts[i].text;
            partText.color = parts[i].color;
            partText.font  = t.font;

            BitmapCi partBmp       = platform.CreateTextTexture(partText);
            int      partWidth     = platform.FloatToInt(platform.BitmapGetWidth(partBmp));
            int      partHeight    = platform.FloatToInt(platform.BitmapGetHeight(partBmp));
            int[]    partBmpPixels = new int[partWidth * partHeight];
            platform.BitmapGetPixelsArgb(partBmp, partBmpPixels);
            for (int x = 0; x < partWidth; x++)
            {
                for (int y = 0; y < partHeight; y++)
                {
                    if (x + currentwidth >= size2X)
                    {
                        continue;
                    }
                    if (y >= size2Y)
                    {
                        continue;
                    }
                    int c = partBmpPixels[MapUtilCi.Index2d(x, y, partWidth)];
                    if (Game.ColorA(c) > 0)
                    {
                        bmp2Pixels[MapUtilCi.Index2d(platform.FloatToInt(currentwidth) + x, y, size2X)] = c;
                    }
                }
            }
            currentwidth += sizeiX;
        }
        platform.BitmapSetPixelsArgb(bmp2, bmp2Pixels);
        return(bmp2);
    }