static void SpawnAsteroid(SpawnAsteroidInfo asteroidInfo)
        {
            if (MySession.Static.CreativeMode || MyEventContext.Current.IsLocallyInvoked || MySession.Static.HasPlayerAdminRights(MyEventContext.Current.Sender.Value))
            {
                MyStorageBase storage;
                string        storageName;
                using (MyRandom.Instance.PushSeed(asteroidInfo.RandomSeed))
                {
                    if (!asteroidInfo.IsProcedural)
                    {
                        var storageNameBase = asteroidInfo.Asteroid + "-" + asteroidInfo.RandomSeed;
                        storageName = MakeStorageName(storageNameBase);
                        storage     = CreateAsteroidStorage(asteroidInfo.Asteroid, asteroidInfo.RandomSeed);
                    }
                    else
                    {
                        var storageNameBase = "ProcAsteroid" + "-" + asteroidInfo.RandomSeed + "r" + asteroidInfo.ProceduralRadius;
                        storageName = MakeStorageName(storageNameBase);
                        storage     = CreateProceduralAsteroidStorage(asteroidInfo.RandomSeed, asteroidInfo.ProceduralRadius, 0.03f);
                    }
                }

                var pastedVoxelMap = new MyVoxelMap();
                pastedVoxelMap.CreatedByUser = true;
                pastedVoxelMap.Save          = true;
                pastedVoxelMap.AsteroidName  = asteroidInfo.Asteroid;
                pastedVoxelMap.Init(storageName, storage, asteroidInfo.Position - storage.Size * 0.5f);
                MyEntities.Add(pastedVoxelMap);
                MyEntities.RaiseEntityCreated(pastedVoxelMap);
            }
        }
Exemple #2
0
        static void SpawnAsteroidConfirmedSuccess(ref SpawnAsteroidConfirmedMsg msg, MyNetworkClient sender)
        {
            MyStorageBase    storage;
            SpawnAsteroidMsg asteroid = msg.AsteroidDetails;

            string storageName;

            using (MyRandom.Instance.PushSeed(asteroid.RandomSeed))
            {
                if (!asteroid.IsProcedural)
                {
                    var storageNameBase = asteroid.StorageName ?? (asteroid.Asteroid + "-" + asteroid.RandomSeed);
                    storageName = MakeStorageName(storageNameBase);
                    storage     = CreateAsteroidStorage(asteroid.Asteroid, asteroid.RandomSeed);
                }
                else
                {
                    var storageNameBase = asteroid.StorageName ?? "ProcAsteroid" + "-" + asteroid.RandomSeed + "r" + asteroid.ProceduralRadius;
                    storageName = MakeStorageName(storageNameBase);
                    storage     = CreateProceduralAsteroidStorage(asteroid.RandomSeed, asteroid.ProceduralRadius, 0.03f);
                }
            }

            var pastedVoxelMap = new MyVoxelMap();

            pastedVoxelMap.EntityId = msg.EntityId;
            pastedVoxelMap.Init(storageName, storage, asteroid.Position - storage.Size * 0.5f);
            MyEntities.Add(pastedVoxelMap);
        }
        public static MyVoxelMap AddVoxelMap(string storageName, MyStorageBase storage, Vector3 positionMinCorner, long entityId = 0)
        {
            var voxelMap = new MyVoxelMap();

            voxelMap.EntityId = entityId;
            voxelMap.Init(storageName, storage, positionMinCorner);
            MyEntities.Add(voxelMap);
            return(voxelMap);
        }
Exemple #4
0
        public static MyVoxelMap AddVoxelMap(string storageName, MyStorageBase storage, MatrixD worldMatrix, long entityId = 0)
        {
            var voxelMap = new MyVoxelMap();

            voxelMap.EntityId = entityId;
            voxelMap.Init(storageName, storage, worldMatrix);
            MyEntities.Add(voxelMap);
            MyEntities.RaiseEntityCreated(voxelMap);
            return(voxelMap);
        }
        public static MyVoxelMap AddVoxelMap(string storageName, MyStorageBase storage, Vector3D positionMinCorner, long entityId = 0)
        {
            var voxelMap = new MyVoxelMap();

            if (entityId != 0)
            {
                voxelMap.EntityId = entityId;
            }
            voxelMap.Init(storageName, storage, positionMinCorner);
            MyEntities.RaiseEntityCreated(voxelMap);
            MyEntities.Add(voxelMap);
            voxelMap.IsReadyForReplication = true;
            return(voxelMap);
        }
        /// <summary>
        /// Builds an asteroid Voxel. Voxel detail will be completed by function callbacks.
        /// This allows for muti-threading, and generating content via algorithims.
        /// </summary>
        /// <param name="multiThread"></param>
        /// <param name="size"></param>
        /// <param name="material"></param>
        /// <param name="faceMaterial"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static MyVoxelMap BuildAsteroid(bool multiThread, Vector3I size, string material, string faceMaterial, Action <MyVoxelBuilderArgs> func)
        {
            var voxelMap   = new MyVoxelMap();
            var actualSize = new Vector3I(size.X.RoundUpToNearest(64), size.Y.RoundUpToNearest(64), size.Z.RoundUpToNearest(64));

            voxelMap.Init(VRageMath.Vector3D.Zero, actualSize, material);

            ProcessAsteroid(voxelMap, multiThread, material, func, false);

            if (faceMaterial != null)
            {
                voxelMap.ForceVoxelFaceMaterial(faceMaterial);
            }

            return(voxelMap);
        }
        public static MyVoxelMap AddVoxelMap(string storageName, MyStorageBase storage, MatrixD worldMatrix, long entityId = 0, bool lazyPhysics = false)
        {
            ProfilerShort.Begin("AddVoxelMap");

            var voxelMap = new MyVoxelMap();

            if (entityId != 0)
            {
                voxelMap.EntityId = entityId;
            }
            voxelMap.DelayRigidBodyCreation = lazyPhysics;
            voxelMap.Init(storageName, storage, worldMatrix);
            MyEntities.Add(voxelMap);
            MyEntities.RaiseEntityCreated(voxelMap);

            ProfilerShort.End();
            return(voxelMap);
        }
        public void RotateAsteroid(VRageMath.Quaternion quaternion)
        {
            var sourceFile = SourceVoxelFilepath ?? VoxelFilepath;

            var asteroid = new MyVoxelMap();

            asteroid.Load(sourceFile, SpaceEngineersCore.Resources.GetDefaultMaterialName(), true);

            var newAsteroid = new MyVoxelMap();
            var transSize   = Vector3I.Transform(asteroid.Size, quaternion);
            var newSize     = Vector3I.Abs(transSize);

            newAsteroid.Init(Vector3D.Zero, newSize, SpaceEngineersCore.Resources.GetDefaultMaterialName());

            Vector3I coords;

            for (coords.Z = 0; coords.Z < asteroid.Size.Z; coords.Z++)
            {
                for (coords.Y = 0; coords.Y < asteroid.Size.Y; coords.Y++)
                {
                    for (coords.X = 0; coords.X < asteroid.Size.X; coords.X++)
                    {
                        byte   volume = 0xff;
                        string cellMaterial;

                        asteroid.GetVoxelMaterialContent(ref coords, out cellMaterial, out volume);

                        var newCoord = Vector3I.Transform(coords, quaternion);
                        // readjust the points, as rotation occurs arround 0,0,0.
                        newCoord.X = newCoord.X < 0 ? newCoord.X - transSize.X : newCoord.X;
                        newCoord.Y = newCoord.Y < 0 ? newCoord.Y - transSize.Y : newCoord.Y;
                        newCoord.Z = newCoord.Z < 0 ? newCoord.Z - transSize.Z : newCoord.Z;
                        newAsteroid.SetVoxelContent(volume, ref newCoord);
                        newAsteroid.SetVoxelMaterialAndIndestructibleContent(cellMaterial, 0xff, ref newCoord);
                    }
                }
            }

            var tempfilename = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension);

            newAsteroid.Save(tempfilename);

            SourceVoxelFilepath = tempfilename;
        }
        public MyObjectBuilder_EntityBase BuildEntity()
        {
            // Realign both asteroids to a common grid, so voxels can be lined up.
            Vector3I roundedPosLeft  = SelectionLeft.WorldAABB.Min.RoundToVector3I();
            Vector3D offsetPosLeft   = SelectionLeft.WorldAABB.Min - (Vector3D)roundedPosLeft;   // Use for everything.
            Vector3I roundedPosRight = (SelectionRight.WorldAABB.Min - offsetPosLeft).RoundToVector3I();
            Vector3D offsetPosRight  = SelectionRight.WorldAABB.Min - (Vector3D)roundedPosRight; // Use for everything.

            // calculate smallest allowable size for contents of both.
            const int paddCells = 3;

            var minLeft      = SelectionLeft.WorldAABB.Min + SelectionLeft.ContentBounds.Min - offsetPosLeft;
            var minRight     = SelectionRight.WorldAABB.Min + SelectionRight.ContentBounds.Min - offsetPosRight;
            var min          = Vector3D.Zero;
            var posOffset    = Vector3D.Zero;
            var asteroidSize = Vector3I.Zero;

            switch (VoxelMergeType)
            {
            case VoxelMergeType.UnionVolumeLeftToRight:
            case VoxelMergeType.UnionVolumeRightToLeft:
                min = Vector3D.Min(minLeft, minRight) - paddCells;
                var max = Vector3D.Max(SelectionLeft.WorldAABB.Min + SelectionLeft.ContentBounds.Max - offsetPosLeft, SelectionRight.WorldAABB.Min + SelectionRight.ContentBounds.Max - offsetPosRight) + paddCells;
                posOffset = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z);
                var size = (max - min).RoundToVector3I();
                asteroidSize = new Vector3I(size.X.RoundUpToNearest(64), size.Y.RoundUpToNearest(64), size.Z.RoundUpToNearest(64));
                break;

            case VoxelMergeType.UnionMaterialLeftToRight:
                min          = SelectionRight.WorldAABB.Min - offsetPosRight;
                posOffset    = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z);
                asteroidSize = SelectionRight.Size;
                break;

            case VoxelMergeType.UnionMaterialRightToLeft:
                min          = SelectionLeft.WorldAABB.Min - offsetPosLeft;
                posOffset    = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z);
                asteroidSize = SelectionLeft.Size;
                break;

            case VoxelMergeType.SubtractVolumeLeftFromRight:
                min          = SelectionRight.WorldAABB.Min - offsetPosRight;
                posOffset    = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z);
                asteroidSize = SelectionRight.Size;
                break;

            case VoxelMergeType.SubtractVolumeRightFromLeft:
                min          = SelectionLeft.WorldAABB.Min - offsetPosLeft;
                posOffset    = new Vector3D(minLeft.X < minRight.X ? offsetPosLeft.X : offsetPosRight.X, minLeft.Y < minRight.Y ? offsetPosLeft.Y : offsetPosRight.Y, minLeft.Z < minRight.Z ? offsetPosLeft.Z : offsetPosRight.Z);
                asteroidSize = SelectionLeft.Size;
                break;
            }

            // Prepare new asteroid.
            var newAsteroid = new MyVoxelMap();

            newAsteroid.Init(Vector3D.Zero, asteroidSize, SpaceEngineersCore.Resources.GetDefaultMaterialName());
            newAsteroid.RemoveContent();
            if (string.IsNullOrEmpty(MergeFileName))
            {
                MergeFileName = "merge";
            }
            var filename = MainViewModel.CreateUniqueVoxelStorageName(MergeFileName);

            // merge.
            switch (VoxelMergeType)
            {
            case VoxelMergeType.UnionVolumeLeftToRight:
                MergeAsteroidVolumeInto(ref newAsteroid, min, SelectionRight, SelectionLeft, minRight, minLeft);
                break;

            case VoxelMergeType.UnionVolumeRightToLeft:
                MergeAsteroidVolumeInto(ref newAsteroid, min, SelectionLeft, SelectionRight, minLeft, minRight);
                break;

            case VoxelMergeType.UnionMaterialLeftToRight:
                MergeAsteroidMaterialFrom(ref newAsteroid, min, SelectionRight, SelectionLeft, minRight, minLeft);
                break;

            case VoxelMergeType.UnionMaterialRightToLeft:
                MergeAsteroidMaterialFrom(ref newAsteroid, min, SelectionLeft, SelectionRight, minLeft, minRight);
                break;

            case VoxelMergeType.SubtractVolumeLeftFromRight:
                SubtractAsteroidVolumeFrom(ref newAsteroid, min, SelectionRight, SelectionLeft, minRight, minLeft);
                break;

            case VoxelMergeType.SubtractVolumeRightFromLeft:
                SubtractAsteroidVolumeFrom(ref newAsteroid, min, SelectionLeft, SelectionRight, minLeft, minRight);
                break;
            }

            // Generate Entity
            var tempfilename = TempfileUtil.NewFilename(MyVoxelMap.V2FileExtension);

            newAsteroid.Save(tempfilename);
            SourceFile = tempfilename;

            var position = min + posOffset;
            var entity   = new MyObjectBuilder_VoxelMap(position, filename)
            {
                EntityId               = SpaceEngineersApi.GenerateEntityId(IDType.ASTEROID),
                PersistentFlags        = MyPersistentEntityFlags2.CastShadows | MyPersistentEntityFlags2.InScene,
                StorageName            = Path.GetFileNameWithoutExtension(filename),
                PositionAndOrientation = new MyPositionAndOrientation
                {
                    Position = position,
                    Forward  = Vector3.Forward,
                    Up       = Vector3.Up
                }
            };

            return(entity);
        }