Esempio n. 1
0
        private BoundingSphereD FindBoundingSphere(MyObjectBuilder_CubeGrid[] grids)
        {
            BoundingSphere result = new BoundingSphere(Vector3.Zero, float.MinValue);

            foreach (MyObjectBuilder_CubeGrid myObjectBuilder_CubeGrid in grids)
            {
                BoundingSphere boundingSphere = MyCubeGridExtensions.CalculateBoundingSphere(myObjectBuilder_CubeGrid);
                MatrixD        m = myObjectBuilder_CubeGrid.PositionAndOrientation.HasValue ? myObjectBuilder_CubeGrid.PositionAndOrientation.Value.GetMatrix() : MatrixD.Identity;
                result.Include(boundingSphere.Transform(m));
            }
            return(result);
        }
        public static bool CopyBlueprintPrefabToClipboard(MyObjectBuilder_Definitions prefab, MyGridClipboard clipboard, bool setOwner = true)
        {
            if (prefab.ShipBlueprints == null)
            {
                return(false);
            }

            var cubeGrids = prefab.ShipBlueprints[0].CubeGrids;

            if (cubeGrids == null || cubeGrids.Count() == 0)
            {
                return(false);
            }

            var localBB = MyCubeGridExtensions.CalculateBoundingSphere(cubeGrids[0]);

            var posAndOrient            = cubeGrids[0].PositionAndOrientation.Value;
            var worldMatrix             = MatrixD.CreateWorld(posAndOrient.Position, posAndOrient.Forward, posAndOrient.Up);
            var invertedNormalizedWorld = Matrix.Normalize(Matrix.Invert(worldMatrix));

            var worldBB = localBB.Transform(worldMatrix);

            var dragVector   = Vector3.TransformNormal((Vector3)(Vector3D)posAndOrient.Position - worldBB.Center, invertedNormalizedWorld);
            var dragDistance = localBB.Radius + 10f;

            //Reset ownership to local player
            if (setOwner)
            {
                foreach (var gridBuilder in cubeGrids)
                {
                    foreach (var block in gridBuilder.CubeBlocks)
                    {
                        if (block.Owner != 0)
                        {
                            block.Owner = MySession.LocalPlayerId;
                        }
                    }
                }
            }

            clipboard.SetGridFromBuilders(cubeGrids, dragVector, dragDistance);
            clipboard.Deactivate();
            return(true);
        }