Пример #1
0
        public void SetBlock(int x, int y, int z, Block block, bool isInRange = false)
        {
            if (!isInRange && !IsInRange(x, y, z))
            {
                chunk.SetBlock(x, chunkOffsetY + y, z, block);
            }
            else
            {
                Block b = GetRealBlock(x, y, z);
                BlockAttributeCalculator old           = BlockAttributeCalculatorFactory.GetCalculator(b.BlockType);
                BlockRenderType          oldRenderType = old.GetBlockRenderType(b.ExtendId);
                if (oldRenderType != BlockRenderType.None)
                {
                    visibleBlocks--;
                }

                if (oldRenderType == BlockRenderType.Part)
                {
                    specialBlocks--;
                }
                BlockAttributeCalculator cur           = BlockAttributeCalculatorFactory.GetCalculator(block.BlockType);
                BlockRenderType          curRenderType = cur.GetBlockRenderType(block.ExtendId);
                if (curRenderType != BlockRenderType.None)
                {
                    visibleBlocks++;
                }
                if (curRenderType == BlockRenderType.Part)
                {
                    specialBlocks++;
                }
                SetRealBlock(x, y, z, block);
                chunk.isUpdate = true;
            }
        }
Пример #2
0
 protected void setBlock(Chunk chunk, int x, int y, int z, Block block, bool isInrange = false)
 {
     chunk.SetBlock(x, y, z, block, isInrange);
     if (isGrow)
     {
         decorationBlockList.Add(new Vector3(x, y, z));
     }
 }
Пример #3
0
 public void removeDecoration(Chunk chunk)
 {
     if (isGrow)
     {
         int length = decorationBlockList.ToArray().Length;
         for (int i = 0; i < length; i++)
         {
             chunk.SetBlock((int)decorationBlockList[i].x, (int)decorationBlockList[i].y, (int)decorationBlockList[i].z, new Block(BlockType.Air));
         }
     }
 }
        public void SetBlock(int x, int y, int z, Block block)
        {
            Chunk chunk = GetChunk(x, y, z);

            if (chunk != null && chunk.isTerrainDataPrepared)
            {
                int tempX = x - chunk.worldPos.x;
                int tempY = y - chunk.worldPos.y;
                int tempZ = z - chunk.worldPos.z;
                chunk.SetBlock(tempX, tempY, tempZ, block, true);
            }
        }
        public bool Decorade(Chunk chunk, int x, int y, int z, IMTBRandom random)
        {
//			int height = random.Range(1,3);
//			int i = 0;
//			for (i = 0; i < height; i++) {
//				if(chunk.GetBlock(x,y + i,z,true).BlockType != BlockType.Air)
//				{
//					break;
//				}
//			}
//			if(i == height)
//			{
//				for (i = 0; i < height; i++) {
//					chunk.SetBlock(x,y + i,z,new Block(BlockType.Block_50));
//				}
//				return true;
//			}
//			return false;

            Vector3[] posData = totalData[random.Range(0, totalData.Length)][random.Range(0, 4)];
            for (int i = 0; i < posData.Length; i++)
            {
                if (chunk.GetBlock(x + (int)posData[i].x, y + (int)posData[i].y, z + (int)posData[i].z).BlockType != BlockType.Air)
                {
                    return(false);
                }
                if (posData[i].y == 0)
                {
                    if (chunk.GetBlock(x + (int)posData[i].x, y + (int)posData[i].y - 1, z + (int)posData[i].z).BlockType == BlockType.Air)
                    {
                        return(false);
                    }
                }
            }
            for (int i = 0; i < posData.Length; i++)
            {
                chunk.SetBlock(x + (int)posData[i].x, y + (int)posData[i].y, z + (int)posData[i].z, new Block(BlockType.Block_50));
            }

            return(false);
        }
Пример #6
0
        protected void generateCaveNode(int seed, Chunk chunk, double x, double y, double z,
                                        float paramFloat1, float paramFloat2, float paramFloat3, int angle, int maxAngle, double paramDouble4)
        {
            int real_x = chunk.worldPos.x + Chunk.chunkWidth / 2;
            int real_z = chunk.worldPos.z + Chunk.chunkDepth / 2;

            float f1 = 0.0F;
            float f2 = 0.0F;

            System.Random localRandom = new System.Random(seed);

            if (maxAngle <= 0)
            {
                int checkAreaSize = CaveHorizontallyExtending * 16 - 16;
                maxAngle = checkAreaSize - localRandom.Next(checkAreaSize / 4);
            }
            bool isLargeCave = false;

            if (angle == -1)
            {
                angle       = maxAngle / 2;
                isLargeCave = true;
            }
            int j = localRandom.Next(maxAngle / 2) + maxAngle / 4;
            int k = localRandom.Next(6) == 0 ? 1 : 0;

            for (; angle < maxAngle; angle++)
            {
                double d3 = 1.5D + Math.Sin(angle * 3.141593F / maxAngle) * paramFloat1 * 1.0F;
                double d4 = d3 - paramDouble4;

                float f3 = (float)Math.Cos(paramFloat3);
                float f4 = (float)Math.Cos(paramFloat3);
                x += Math.Cos(paramFloat2) * f3;
                y += 0.3F;
                z += Math.Sin(paramFloat2) * f3;

                if (k != 0)
                {
                    paramFloat3 *= 0.92F;
                }
                else
                {
                    paramFloat3 *= 0.7F;
                }
                paramFloat3 += f2 * 0.1F;
                paramFloat2 += f1 * 0.1F;

                f2 *= 0.9F;
                f1 *= 0.75F;
                f2 += (float)(localRandom.NextDouble() - localRandom.NextDouble()) * (float)localRandom.NextDouble() * 2.0F;
                f1 += (float)(localRandom.NextDouble() - localRandom.NextDouble()) * (float)localRandom.NextDouble() * 4.0F;

                if ((!isLargeCave) && (angle == j) && (paramFloat1 > 1.0F) && (maxAngle > 0))
                {
                    generateCaveNode(localRandom.Next(), chunk, x, y, z, (float)localRandom.NextDouble() * 0.5F + 0.5F,
                                     paramFloat2 - 1.570796F, paramFloat3 / 3.0F, angle, maxAngle, 1.0D);
                    generateCaveNode(localRandom.Next(), chunk, x, y, z, (float)localRandom.NextDouble() * 0.5F + 0.5F,
                                     paramFloat2 + 1.570796F, paramFloat3 / 3.0F, angle, maxAngle, 1.0D);
                    return;
                }

                if ((!isLargeCave) && (localRandom.Next(4) == 0))
                {
                    continue;
                }

                double d5 = x - real_x;
                double d6 = z - real_z;
                double d7 = maxAngle - angle;
                double d8 = paramFloat1 + 2.0F + 16.0F;

                if (d5 * d5 + d6 * d6 - d7 * d7 > d8 * d8)
                {
                    return;
                }

                if ((x < real_x - 16.0D - d3 * 2.0D) || (z < real_z - 16.0D - d3 * 2.0D) || (x > real_x + 16.0D + d3 * 2.0D) || (z > real_z + 16.0D + d3 * 2.0D))
                {
                    continue;
                }

                int m = Int32.Parse(Math.Floor(x - d3).ToString()) - chunk.worldPos.x - 1;
                int n = Int32.Parse(Math.Floor(x + d3).ToString()) - chunk.worldPos.x + 1;

                int i1 = Int32.Parse(Math.Floor(y - d4).ToString()) - 1;
                int i2 = Int32.Parse(Math.Floor(y + d4).ToString()) + 1;

                int i3 = Int32.Parse(Math.Floor(z - d3).ToString()) - chunk.worldPos.z - 1;
                int i4 = Int32.Parse(Math.Floor(z + d3).ToString()) - chunk.worldPos.z + 1;

                m  = m < 0 ? 0 : m;
                n  = n > 16 ? 16 : n;
                i1 = i1 < 1 ? 1 : i1;
                i2 = i2 > CaveMaxAltitude - 8 ? CaveMaxAltitude - 8 : i2;
                i3 = i3 < 0 ? 0 : i3;
                i4 = i4 > 16 ? 16 : i4;

                // Generate cave
                for (int local_x = m; local_x < n; local_x++)
                {
                    double d9 = (local_x + chunk.worldPos.x + 0.5D - x) / d3;
                    for (int local_z = i3; local_z < i4; local_z++)
                    {
                        double d10 = (local_z + chunk.worldPos.z + 0.5D - z) / d3;
                        if (d9 * d9 + d10 * d10 < 1.0D)
                        {
                            for (int local_y = i2; local_y > CaveLowCut && local_y < CaveHighCut && local_y > i1; local_y--)
                            {
                                double d11 = ((local_y - 1) + 0.5D - y) / d4;
                                if ((d11 > -2.0D) && (d9 * d9 + d11 * d11 + d10 * d10 < 2.5D))
                                {
                                    chunk.SetBlock(local_x, local_y, local_z, new Block(BlockType.Air));
                                }
                            }
                        }
                    }
                }
                if (isLargeCave)
                {
                    break;
                }
            }
        }
        private void AddBiomeBlocks(Chunk chunk)
        {
            int dryBlocksOnSurface = 256;
            int chunkX             = chunk.worldPos.x;
            int chunkZ             = chunk.worldPos.z;

            for (int x = 0; x < Chunk.chunkWidth; x++)
            {
                for (int z = 0; z < Chunk.chunkDepth; z++)
                {
                    BiomeConfig biomeConfig = WorldConfig.Instance.GetBiomeConfigById(this.biomeArray[x + Chunk.chunkDepth * z]);
//					int surfaceBlocksNoise = (int)_surfaceNoiseGen.GetValue(chunkX + x,chunkZ + z);
                    int   surfaceBlocksNoise  = 1;
                    Block currentSurfaceBlock = biomeConfig.surfaceBlock;
                    Block currentGroundBlock  = biomeConfig.groundBlock;
                    int   surfaceBlocksCount  = int.MinValue;
                    int   currentWaterLevel   = this.waterLevel[x * Chunk.chunkDepth + z];
                    for (int y = heightCap - 1; y >= 0; y--)
                    {
                        Block curBlock = chunk.GetBlock(x, y, z, true);
                        if (curBlock.BlockType == BlockType.Air)
                        {
                            surfaceBlocksCount = int.MinValue;
                        }
                        else if (curBlock.EqualOther(biomeConfig.stoneBlock))
                        {
                            if (surfaceBlocksCount == int.MinValue)
                            {
                                if (surfaceBlocksNoise <= 0 && biomeConfig.removeSurfaceStone)
                                {
                                    currentSurfaceBlock = air;
                                    currentGroundBlock  = biomeConfig.groundBlock;
                                }

                                /*else if((y > currentWaterLevel - 3) && (y < currentWaterLevel + 2))
                                 * {
                                 *      currentSurfaceBlock = biomeConfig.surfaceBlock;
                                 *      currentGroundBlock = biomeConfig.groundBlock;
                                 * }
                                 */
                                if ((y < currentWaterLevel) && (y > biomeConfig.waterLevelMin) && currentSurfaceBlock.BlockType == BlockType.Air)
                                {
                                    currentSurfaceBlock = biomeConfig.waterBlock;
                                }
                                surfaceBlocksCount = surfaceBlocksNoise;
                                if (surfaceBlocksNoise <= 0)
                                {
                                    surfaceBlocksCount += 3;
                                }
                                if (y > currentWaterLevel - 2)
                                {
                                    chunk.SetBlock(x, y, z, currentSurfaceBlock, true);
                                }
                                else
                                {
                                    chunk.SetBlock(x, y, z, currentGroundBlock, true);
                                }
                            }
                            else if (surfaceBlocksCount > 0)
                            {
                                surfaceBlocksCount--;
                                chunk.SetBlock(x, y, z, currentGroundBlock, true);
                            }
                        }
                    }
                }
            }
        }
        private void GenerateBiomeAndTerrain(Chunk chunk)
        {
            int chunkX = chunk.worldPos.x;
            int chunkZ = chunk.worldPos.z;

            int maxYSections     = heightCap / 8 + 1;
            int oneEightOfHeight = heightCap / 8;

            this.biomeArray = _biomeGenerator.GetBiomesUnZoomed(this.biomeArray, chunkX / 4 - this.maxSmoothRadius, chunkZ / 4 - this.maxSmoothRadius,
                                                                NOISE_MAX_X + this.maxSmoothDiameter, NOISE_MAX_Z + this.maxSmoothDiameter);

            GenerateTerrainNoise(chunkX / 4, 0, chunkZ / 4, maxYSections);


            this.biomeArray = _biomeGenerator.GetBiomes(this.biomeArray, chunkX, chunkZ, Chunk.chunkWidth, Chunk.chunkDepth);

            float oneOfEight  = 0.125f;
            float oneOfFourth = 0.25f;

            for (int x = 0; x < NOISE_MAX_X - 1; x++)
            {
                for (int z = 0; z < NOISE_MAX_Z - 1; z++)
                {
                    float waterLevel_x0z0 = this.waterLevelRaw[x * NOISE_MAX_Z + z] & 0xff;
                    float waterLevel_x0z1 = this.waterLevelRaw[x * NOISE_MAX_Z + (z + 1)] & 0xff;
                    float waterLevel_x1z0 = ((this.waterLevelRaw[(x + 1) * NOISE_MAX_Z + z] & 0xff) - waterLevel_x0z0) * oneOfFourth;
                    float waterLevel_x1z1 = ((this.waterLevelRaw[(x + 1) * NOISE_MAX_Z + (z + 1)] & 0xff) - waterLevel_x0z1) * oneOfFourth;
                    for (int x_piece = 0; x_piece < 4; x_piece++)
                    {
                        float waterLevelForArray = waterLevel_x0z0;
                        float subZ1_0            = (waterLevel_x0z1 - waterLevel_x0z0) * oneOfFourth;
                        for (int z_piece = 0; z_piece < 4; z_piece++)
                        {
                            this.waterLevel[(x * 4 + x_piece) * Chunk.chunkDepth + (z * 4 + z_piece)] = (byte)waterLevelForArray;
                            waterLevelForArray += subZ1_0;
                        }
                        waterLevel_x0z0 += waterLevel_x1z0;
                        waterLevel_x0z1 += waterLevel_x1z1;
                    }

                    for (int y = 0; y < oneEightOfHeight; y++)
                    {
                        float x0z0 = this.rawTerrain[(x * NOISE_MAX_Z + z) * maxYSections + y];
                        float x0z1 = this.rawTerrain[(x * NOISE_MAX_Z + (z + 1)) * maxYSections + y];
                        float x1z0 = this.rawTerrain[((x + 1) * NOISE_MAX_Z + z) * maxYSections + y];
                        float x1z1 = this.rawTerrain[((x + 1) * NOISE_MAX_Z + (z + 1)) * maxYSections + y];

                        float x0z0y1 = (this.rawTerrain[(x * NOISE_MAX_Z + z) * maxYSections + (y + 1)] - x0z0) * oneOfEight;
                        float x0z1y1 = (this.rawTerrain[(x * NOISE_MAX_Z + (z + 1)) * maxYSections + (y + 1)] - x0z1) * oneOfEight;
                        float x1z0y1 = (this.rawTerrain[((x + 1) * NOISE_MAX_Z + z) * maxYSections + (y + 1)] - x1z0) * oneOfEight;
                        float x1z1y1 = (this.rawTerrain[((x + 1) * NOISE_MAX_Z + (z + 1)) * maxYSections + (y + 1)] - x1z1) * oneOfEight;

                        for (int y_piece = 0; y_piece < 8; y_piece++)
                        {
                            float d11 = x0z0;
                            float d12 = x0z1;
                            float d13 = (x1z0 - x0z0) * oneOfFourth;
                            float d14 = (x1z1 - x0z1) * oneOfFourth;
                            for (int x_piece = 0; x_piece < 4; x_piece++)
                            {
                                float result  = d11;
                                float subZ1_0 = (d12 - d11) * oneOfFourth;
                                for (int z_piece = 0; z_piece < 4; z_piece++)
                                {
                                    int tx = x * 4 + x_piece;
                                    int ty = y * 8 + y_piece;
                                    int tz = z * 4 + z_piece;
                                    terrain[(tx * Chunk.chunkDepth + tz) * heightCap + ty] = result;
//									terrain[x * 4 + x_piece,y * 8 + y_piece,z * 4 + z_piece] = result;
                                    result += subZ1_0;
                                }
                                d11 += d13;
                                d12 += d14;
                            }
                            x0z0 += x0z0y1;
                            x0z1 += x0z1y1;
                            x1z0 += x1z0y1;
                            x1z1 += x1z1y1;
                        }
                    }
                }
            }


            for (int x = 0; x < Chunk.chunkWidth; x++)
            {
                for (int z = 0; z < Chunk.chunkDepth; z++)
                {
                    BiomeConfig biomeConfig   = WorldConfig.Instance.GetBiomeConfigById(this.biomeArray[x + Chunk.chunkDepth * z]);
                    int         curWaterLevel = waterLevel[x * Chunk.chunkDepth + z] & 0xff;
                    for (int y = Chunk.chunkHeight - 1; y >= 0; y--)
                    {
                        Block b = air;
                        if (y < heightCap)
                        {
                            if (y < curWaterLevel && y > biomeConfig.waterLevelMin)
                            {
                                b = water;
                            }

                            if (terrain[(x * Chunk.chunkDepth + z) * heightCap + y] > 0f)
                            {
                                b = biomeConfig.stoneBlock;
                            }
                        }
                        chunk.SetBlock(x, y, z, b, true);
                    }
                }
            }
        }
Пример #9
0
        protected virtual void generatorWormBlocks(Chunk chunk, Vector3 point)
        {
            int x  = Int32.Parse(point.x.ToString());
            int y  = Int32.Parse(point.y.ToString());
            int z  = Int32.Parse(point.z.ToString());
            int tx = x - _radiusWidth;
            int ty = y - _radiusHeight;
            int tz = z - _radiusWidth;

            for (tx = x - _radiusWidth; tx <= x + _radiusWidth; tx++)
            {
                if (tx >= 0 && tx < Chunk.chunkWidth)
                {
                    for (tz = z - _radiusWidth; tz <= z + _radiusWidth; tz++)
                    {
                        if (tz >= 0 && tz < Chunk.chunkDepth)
                        {
                            for (ty = y - _radiusHeight; ty <= y + _radiusHeight; ty++)
                            {
                                if (ty >= 0 && ty < Chunk.chunkHeight)
                                {
                                    chunk.SetBlock(tx, ty, tz, new Block(BlockType.Air), true);
                                }
                            }
                        }
                    }
                }
            }

            //墙面不规则处理差值
            float flu, fld, fru, frd, blu, bld, bru, brd;
            float lerp1, lerp2, lerp3;

            flu = 200 * (float)_sectionGenerator.GetValue(x - _radiusWidth, y + _radiusHeight, z + _radiusWidth);
            fld = 200 * (float)_sectionGenerator.GetValue(x - _radiusWidth, y - _radiusHeight, z + _radiusWidth);
            fru = 200 * (float)_sectionGenerator.GetValue(x + _radiusWidth, y + _radiusHeight, z + _radiusWidth);
            frd = 200 * (float)_sectionGenerator.GetValue(x + _radiusWidth, y - _radiusHeight, z + _radiusWidth);
            blu = 200 * (float)_sectionGenerator.GetValue(x - _radiusWidth, y + _radiusHeight, z - _radiusWidth);
            bld = 200 * (float)_sectionGenerator.GetValue(x - _radiusWidth, y - _radiusHeight, z - _radiusWidth);
            bru = 200 * (float)_sectionGenerator.GetValue(x + _radiusWidth, y + _radiusHeight, z - _radiusWidth);
            brd = 200 * (float)_sectionGenerator.GetValue(x + _radiusWidth, y - _radiusHeight, z - _radiusWidth);

            //左侧
            tx = x - _radiusWidth - 1;
            if (tx >= 0 && tx < Chunk.chunkWidth)
            {
                for (tz = z - _radiusWidth - 1; tz <= z + _radiusWidth + 1; tz++)
                {
                    if (tz >= 0 && tz < Chunk.chunkDepth)
                    {
                        for (ty = y - _radiusHeight - 1; ty <= y + _radiusHeight + 1; ty++)
                        {
                            if (ty >= 0 && ty < Chunk.chunkHeight)
                            {
                                lerp1 = Mathf.Lerp(blu, flu, (float)(tz - z + _radiusWidth + 1) / (float)(_radiusWidth * 2 + 2));
                                lerp2 = Mathf.Lerp(bld, fld, (float)(tz - z + _radiusWidth + 1) / (float)(_radiusWidth * 2 + 2));
                                lerp3 = Mathf.Lerp(lerp2, lerp1, (float)(ty - y + _radiusHeight + 1) / (float)(_radiusHeight * 2 + 2));
                                if (lerp3 > _wallThreshold)
                                {
                                    chunk.SetBlock(tx, ty, tz, new Block(BlockType.Air), true);
                                }
                            }
                        }
                    }
                }
            }

            //右侧
            tx = x + _radiusWidth + 1;
            if (tx >= 0 && tx < Chunk.chunkWidth)
            {
                for (tz = z - _radiusWidth - 1; tz <= z + _radiusWidth + 1; tz++)
                {
                    if (tz >= 0 && tz < Chunk.chunkDepth)
                    {
                        for (ty = y - _radiusHeight - 1; ty <= y + _radiusHeight + 1; ty++)
                        {
                            if (ty >= 0 && ty < Chunk.chunkHeight)
                            {
                                lerp1 = Mathf.Lerp(bru, fru, (float)(tz - z + _radiusWidth + 1) / (float)(_radiusWidth * 2 + 2));
                                lerp2 = Mathf.Lerp(brd, frd, (float)(tz - z + _radiusWidth + 1) / (float)(_radiusWidth * 2 + 2));
                                lerp3 = Mathf.Lerp(lerp2, lerp1, (float)(ty - y + _radiusHeight + 1) / (float)(_radiusHeight * 2 + 2));
                                if (lerp3 > _wallThreshold)
                                {
                                    chunk.SetBlock(tx, ty, tz, new Block(BlockType.Air), true);
                                }
                            }
                        }
                    }
                }
            }

            //前侧
            tz = z + _radiusWidth + 1;
            if (tz >= 0 && tz < Chunk.chunkWidth)
            {
                for (tx = x - _radiusWidth - 1; tx <= x + _radiusWidth + 1; tx++)
                {
                    if (tx >= 0 && tx < Chunk.chunkWidth)
                    {
                        for (ty = y - _radiusHeight - 1; ty <= y + _radiusHeight + 1; ty++)
                        {
                            if (ty >= 0 && ty < Chunk.chunkHeight)
                            {
                                lerp1 = Mathf.Lerp(flu, fru, (float)(tx - x + _radiusWidth + 1) / (float)(_radiusWidth * 2 + 2));
                                lerp2 = Mathf.Lerp(fld, frd, (float)(tx - x + _radiusWidth + 1) / (float)(_radiusWidth * 2 + 2));
                                lerp3 = Mathf.Lerp(lerp2, lerp1, (float)(ty - y + _radiusHeight + 1) / (float)(_radiusHeight * 2 + 2));
                                if (lerp3 > _wallThreshold)
                                {
                                    chunk.SetBlock(tx, ty, tz, new Block(BlockType.Air), true);
                                }
                            }
                        }
                    }
                }
            }

            //后侧
            tz = z - _radiusWidth - 1;
            if (tz >= 0 && tz < Chunk.chunkWidth)
            {
                for (tx = x - _radiusWidth - 1; tx <= x + _radiusWidth + 1; tx++)
                {
                    if (tx >= 0 && tx < Chunk.chunkWidth)
                    {
                        for (ty = y - _radiusHeight - 1; ty <= y + _radiusHeight + 1; ty++)
                        {
                            if (ty >= 0 && ty < Chunk.chunkHeight)
                            {
                                lerp1 = Mathf.Lerp(blu, bru, (float)(tx - x + _radiusWidth + 1) / (float)(_radiusWidth * 2 + 2));
                                lerp2 = Mathf.Lerp(bld, brd, (float)(tx - x + _radiusWidth + 1) / (float)(_radiusWidth * 2 + 2));
                                lerp3 = Mathf.Lerp(lerp2, lerp1, (float)(ty - y + _radiusHeight + 1) / (float)(_radiusHeight * 2 + 2));
                                if (lerp3 > _wallThreshold)
                                {
                                    chunk.SetBlock(tx, ty, tz, new Block(BlockType.Air), true);
                                }
                            }
                        }
                    }
                }
            }
        }