Example #1
0
        //  Merges specified materials (from file) into our actual voxel map - overwriting materials only.
        //  We are using a regular voxel map to define areas where we want to set a specified material. Empty voxels are ignored and 
        //  only mixed/full voxels are used to tell us that that voxel will contain new material - 'materialToSet'.
        //  If we are seting indestructible material, voxel content values from merged voxel map will be used to define indestructible content.
        //  Parameter 'voxelPosition' - place where we will place merged voxel map withing actual voxel map. It's in voxel coords.
        //  IMPORTANT: THIS METHOD WILL WORK ONLY IF WE PLACE THE MAP THAT WE TRY TO MERGE FROM IN VOXEL COORDINATES THAT ARE MULTIPLY OF DATA CELL SIZE
        //  This method is used to load small material areas, overwriting actual material only if value from file is 1. Zeros are ignored (it's empty space).
        //  This method is quite fast, even on large maps - 512x512x512, so we can do more overwrites.
        //  Parameter 'materialToSet' tells us what material to set at places which are full in file. Empty are ignored - so stay as they were before this method was called.
        //  IMPORTANT: THIS MERGE MATERIAL CAN BE CALLED ONLY AFTER ALL VOXEL CONTENTS ARE LOADED. THAT'S BECAUSE WE NEED TO KNOW THEM FOR MIN CONTENT / INDESTRUCTIBLE CONTENT.
        //  Voxel map we are trying to merge into existing voxel map can be bigger or outside of area of existing voxel map. This method will just ignore those parts.
        public void MergeVoxelMaterials(MyMwcVoxelFilesEnum voxelFile, MyMwcVector3Short voxelPosition, MyMwcVoxelMaterialsEnum materialToSet)
        {
            MyMwcLog.WriteLine("MyVoxelMap.MergeVoxelMaterials() - Start");
            MyMwcLog.IncreaseIndent();

            MyCompressionFileLoad decompressFile = new MyCompressionFileLoad(MyVoxelFiles.Get(voxelFile).GetVoxFilePath());

            //  Version of a VOX file
            int fileVersion = decompressFile.GetInt32();

            //  Not supported VOX file version
            MyCommonDebugUtils.AssertRelease(fileVersion == MyVoxelConstants.VOXEL_FILE_ACTUAL_VERSION);

            //  Size of this voxel map (in voxels)
            int sizeX = decompressFile.GetInt32();
            int sizeY = decompressFile.GetInt32();
            int sizeZ = decompressFile.GetInt32();

            //  Size of data cell in voxels, doesn't have to be same as current size specified by our constants.
            int cellSizeX = decompressFile.GetInt32();
            int cellSizeY = decompressFile.GetInt32();
            int cellSizeZ = decompressFile.GetInt32();

            int cellsCountX = sizeX / cellSizeX;
            int cellsCountY = sizeY / cellSizeY;
            int cellsCountZ = sizeZ / cellSizeZ;

            //  This method will work only if we place the map that we try to merge from in voxel coordinates that are multiply of data cell size
            MyCommonDebugUtils.AssertRelease((voxelPosition.X & MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_MASK) == 0);
            MyCommonDebugUtils.AssertRelease((voxelPosition.Y & MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_MASK) == 0);
            MyCommonDebugUtils.AssertRelease((voxelPosition.Z & MyVoxelConstants.VOXEL_DATA_CELL_SIZE_IN_VOXELS_MASK) == 0);
            MyMwcVector3Int cellFullForVoxelPosition = GetDataCellCoordinate(ref voxelPosition);

            MyMwcVector3Int cellCoord;
            for (cellCoord.X = 0; cellCoord.X < cellsCountX; cellCoord.X++)
            {
                for (cellCoord.Y = 0; cellCoord.Y < cellsCountY; cellCoord.Y++)
                {
                    for (cellCoord.Z = 0; cellCoord.Z < cellsCountZ; cellCoord.Z++)
                    {
                        MyVoxelCellType cellType = (MyVoxelCellType)decompressFile.GetByte();

                        //  We can do "continue" here, becase we need to read this file properly, even if we will ignore that data
                        bool isDataCellInVoxelMap = IsDataCellInVoxelMap(
                            new MyMwcVector3Int(
                                cellFullForVoxelPosition.X + cellCoord.X,
                                cellFullForVoxelPosition.Y + cellCoord.Y,
                                cellFullForVoxelPosition.Z + cellCoord.Z));

                        if (cellType == MyVoxelCellType.EMPTY)
                        {
                            //  If merged cell is empty, there is nothing to overwrite, so we can skip this cell
                            continue;
                        }
                        else if (cellType == MyVoxelCellType.FULL)
                        {
                            //  If merged cell is full, than we reset whole material cell to 'materialToSet'
                            if (isDataCellInVoxelMap)
                            {
                                m_voxelMaterialCells[cellFullForVoxelPosition.X + cellCoord.X][cellFullForVoxelPosition.Y +
                                    cellCoord.Y][cellFullForVoxelPosition.Z + cellCoord.Z].Reset(
                                    materialToSet, GetIndestructibleContentByMaterial(materialToSet));
                            }
                        }
                        else
                        {
                            MyMwcVector3Int cellCoordInVoxels = GetVoxelCoordinatesOfDataCell(ref cellCoord);

                            MyMwcVector3Int voxelCoordRelative;
                            voxelCoordRelative.X = voxelPosition.X + cellCoordInVoxels.X;
                            voxelCoordRelative.Y = voxelPosition.Y + cellCoordInVoxels.Y;
                            voxelCoordRelative.Z = voxelPosition.Z + cellCoordInVoxels.Z;

                            MyMwcVector3Int voxelCoordInCell;
                            for (voxelCoordInCell.X = 0; voxelCoordInCell.X < cellSizeX; voxelCoordInCell.X++)
                            {
                                for (voxelCoordInCell.Y = 0; voxelCoordInCell.Y < cellSizeY; voxelCoordInCell.Y++)
                                {
                                    for (voxelCoordInCell.Z = 0; voxelCoordInCell.Z < cellSizeZ; voxelCoordInCell.Z++)
                                    {
                                        byte voxelFromFile = decompressFile.GetByte();

                                        if (isDataCellInVoxelMap)
                                        {
                                            //  Ignore empty voxels, but use mixed/full for seting the material
                                            if (voxelFromFile > MyVoxelConstants.VOXEL_CONTENT_EMPTY)
                                            {
                                                MyMwcVector3Int voxelCoord = new MyMwcVector3Int(
                                                    voxelCoordRelative.X + voxelCoordInCell.X,
                                                    voxelCoordRelative.Y + voxelCoordInCell.Y,
                                                    voxelCoordRelative.Z + voxelCoordInCell.Z);

                                                //  Actual voxel content
                                                byte voxelContent = GetVoxelContent(ref voxelCoord);

                                                //  If this is indestructible material, here we will get 'min content' for this voxel
                                                byte indestructibleContent = GetIndestructibleContentsByMaterialAndContent(materialToSet, voxelFromFile);

                                                //  Indestructible content can be less than real voxel at this place. First I made this mistake.
                                                //  If forgoten, then during explosions we will in fact create matter from 'indestructible content' array.
                                                if (indestructibleContent > voxelContent)
                                                {
                                                    indestructibleContent = voxelContent;
                                                }

                                                SetVoxelMaterialAndIndestructibleContent(materialToSet, indestructibleContent, ref voxelCoord);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            MyMwcLog.DecreaseIndent();
            MyMwcLog.WriteLine("MyVoxelMap.MergeVoxelMaterials() - End");
        }
Example #2
0
        // Not used
        //public void Reinitialize()
        //{
        //    if (m_isClosed)
        //    {
        //        Init(this.Name, this.PositionLeftBottomCorner, (MyMwcObjectBuilder_VoxelMap)this.GetObjectBuilderInternal());
        //        m_isClosed = false;
        //    }
        //}

        //  Merges a specified voxel map (from a file) into our actual voxel map at a specified position. 
        //  This merging is slower than loading voxel map through constructor (because we are setting voxels through SetVoxelContent) - so use it only 
        //  for merging-in small areas.
        //  Parameter 'voxelPosition' - where will be placed new merged voxel map withing actual voxel map. It's in voxel coords.
        //  Voxel map we are trying to merge into existing voxel map can be bigger or outside of area of existing voxel map. This method will just ignore those parts.
        //  Coordinate of 'voxelPosition' DOESN'T NEED to be aligned to data cell (multiplies of 8).
        public void MergeVoxelContents(MyMwcVoxelFilesEnum voxelFile, MyMwcVector3Short voxelPosition, MyMwcVoxelMapMergeTypeEnum mergeType)
        {
            MyCompressionFileLoad decompressFile = new MyCompressionFileLoad(MyVoxelFiles.Get(voxelFile).GetVoxFilePath());

            //  Version of a VOX file
            int fileVersion = decompressFile.GetInt32();

            //  Not supported VOX file version
            MyCommonDebugUtils.AssertRelease(fileVersion == MyVoxelConstants.VOXEL_FILE_ACTUAL_VERSION);

            //  Size of this voxel map (in voxels)
            int sizeX = decompressFile.GetInt32();
            int sizeY = decompressFile.GetInt32();
            int sizeZ = decompressFile.GetInt32();

            //  Size of data cell in voxels, doesn't have to be same as current size specified by our constants.
            int cellSizeX = decompressFile.GetInt32();
            int cellSizeY = decompressFile.GetInt32();
            int cellSizeZ = decompressFile.GetInt32();

            int cellsCountX = sizeX / cellSizeX;
            int cellsCountY = sizeY / cellSizeY;
            int cellsCountZ = sizeZ / cellSizeZ;

            MyMwcVector3Int cellCoord;
            for (cellCoord.X = 0; cellCoord.X < cellsCountX; cellCoord.X++)
            {
                for (cellCoord.Y = 0; cellCoord.Y < cellsCountY; cellCoord.Y++)
                {
                    for (cellCoord.Z = 0; cellCoord.Z < cellsCountZ; cellCoord.Z++)
                    {
                        MyVoxelCellType cellType = (MyVoxelCellType)decompressFile.GetByte();

                        MyMwcVector3Int cellCoordInVoxels = GetVoxelCoordinatesOfDataCell(ref cellCoord);

                        //  Go through every voxel in a cell and change it's value. If cell is empty, set all voxels to empty. Otherwise set by value from file.
                        MyMwcVector3Int voxelCoordInCell;
                        for (voxelCoordInCell.X = 0; voxelCoordInCell.X < cellSizeX; voxelCoordInCell.X++)
                        {
                            for (voxelCoordInCell.Y = 0; voxelCoordInCell.Y < cellSizeY; voxelCoordInCell.Y++)
                            {
                                for (voxelCoordInCell.Z = 0; voxelCoordInCell.Z < cellSizeZ; voxelCoordInCell.Z++)
                                {
                                    byte newContent;
                                    if (cellType == MyVoxelCellType.EMPTY)
                                    {
                                        newContent = MyVoxelConstants.VOXEL_CONTENT_EMPTY;
                                    }
                                    else if (cellType == MyVoxelCellType.FULL)
                                    {
                                        newContent = MyVoxelConstants.VOXEL_CONTENT_FULL;
                                    }
                                    else
                                    {
                                        newContent = decompressFile.GetByte();
                                    }

                                    MyMwcVector3Int voxelCoord;
                                    voxelCoord.X = voxelPosition.X + cellCoordInVoxels.X + voxelCoordInCell.X;
                                    voxelCoord.Y = voxelPosition.Y + cellCoordInVoxels.Y + voxelCoordInCell.Y;
                                    voxelCoord.Z = voxelPosition.Z + cellCoordInVoxels.Z + voxelCoordInCell.Z;

                                    if (IsVoxelInVoxelMap(ref voxelCoord) == true)
                                    {
                                        byte originalContent = GetVoxelContent(ref voxelCoord);

                                        if (mergeType == MyMwcVoxelMapMergeTypeEnum.ADD)
                                        {
                                            //  Set new content only if its value is higher than actual - so we only can add matter
                                            if (newContent > originalContent)
                                            {
                                                SetVoxelContent(newContent, ref voxelCoord);
                                            }
                                        }
                                        else if (mergeType == MyMwcVoxelMapMergeTypeEnum.INVERSE_AND_SUBTRACT)
                                        {
                                            //  Subtract new content from original, so if original voxel is full and new is full too, result will be empty voxel
                                            //  If new is empty, nothing will happen. If new is full but original is empty, nothing will happen either.
                                            SetVoxelContent((byte)MyMwcUtils.GetClampInt((int)originalContent - (int)newContent, MyVoxelConstants.VOXEL_CONTENT_EMPTY, MyVoxelConstants.VOXEL_CONTENT_FULL), ref voxelCoord);
                                        }
                                        else
                                        {
                                            throw new MyMwcExceptionApplicationShouldNotGetHere();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }