示例#1
0
 public bool IsMultiBlockPart(MyDefinitionId multiBlockDefinition, int multiblockId)
 {
     if (this.MultiBlocks != null)
     {
         using (List <MultiBlockPartInfo> .Enumerator enumerator = this.MultiBlocks.GetEnumerator())
         {
             while (true)
             {
                 if (!enumerator.MoveNext())
                 {
                     break;
                 }
                 MultiBlockPartInfo current = enumerator.Current;
                 if ((current != null) && ((current.MultiBlockDefinition == multiBlockDefinition) && (current.MultiBlockId == multiblockId)))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#2
0
        public override void Init(MyObjectBuilder_CubeBlock builder, MyCubeGrid cubeGrid)
        {
            base.Init(builder, cubeGrid);
            base.CheckConnectionAllowed = true;
            MyObjectBuilder_FracturedBlock block = builder as MyObjectBuilder_FracturedBlock;

            if (block.Shapes.Count == 0)
            {
                if (!block.CreatingFracturedBlock)
                {
                    throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed.");
                }
            }
            else
            {
                this.OriginalBlocks = new List <MyDefinitionId>();
                this.Orientations   = new List <MyBlockOrientation>();
                List <HkdShapeInstanceInfo> list = new List <HkdShapeInstanceInfo>();
                foreach (SerializableDefinitionId id in block.BlockDefinitions)
                {
                    MyCubeBlockDefinition cubeBlockDefinition = MyDefinitionManager.Static.GetCubeBlockDefinition(id);
                    string model = cubeBlockDefinition.Model;
                    if (MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                    {
                        MyDestructionData.Static.LoadModelDestruction(model, cubeBlockDefinition, Vector3.One, true, false);
                    }
                    HkdBreakableShape    shape       = MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                    Quaternion?          rotation    = null;
                    Vector3?             translation = null;
                    HkdShapeInstanceInfo item        = new HkdShapeInstanceInfo(shape, rotation, translation);
                    list.Add(item);
                    m_children.Add(item);
                    shape.GetChildren(m_children);
                    if (cubeBlockDefinition.BuildProgressModels != null)
                    {
                        MyCubeBlockDefinition.BuildProgressModel[] buildProgressModels = cubeBlockDefinition.BuildProgressModels;
                        for (int j = 0; j < buildProgressModels.Length; j++)
                        {
                            model = buildProgressModels[j].File;
                            if (MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                            {
                                MyDestructionData.Static.LoadModelDestruction(model, cubeBlockDefinition, Vector3.One, true, false);
                            }
                            shape       = MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                            rotation    = null;
                            translation = null;
                            item        = new HkdShapeInstanceInfo(shape, rotation, translation);
                            list.Add(item);
                            m_children.Add(item);
                            shape.GetChildren(m_children);
                        }
                    }
                    this.OriginalBlocks.Add(id);
                }
                foreach (SerializableBlockOrientation orientation in block.BlockOrientations)
                {
                    this.Orientations.Add((MyBlockOrientation)orientation);
                }
                if (block.MultiBlocks.Count > 0)
                {
                    this.MultiBlocks = new List <MultiBlockPartInfo>();
                    foreach (MyObjectBuilder_FracturedBlock.MyMultiBlockPart part in block.MultiBlocks)
                    {
                        if (part == null)
                        {
                            this.MultiBlocks.Add(null);
                            continue;
                        }
                        MultiBlockPartInfo item = new MultiBlockPartInfo();
                        item.MultiBlockDefinition = part.MultiBlockDefinition;
                        item.MultiBlockId         = part.MultiBlockId;
                        this.MultiBlocks.Add(item);
                    }
                }
                this.m_shapes.AddRange(block.Shapes);
                for (int i = 0; i < m_children.Count; i++)
                {
                    HkdShapeInstanceInfo child = m_children[i];
                    Func <MyObjectBuilder_FracturedBlock.ShapeB, bool>  predicate = s => s.Name == child.ShapeName;
                    IEnumerable <MyObjectBuilder_FracturedBlock.ShapeB> source    = this.m_shapes.Where <MyObjectBuilder_FracturedBlock.ShapeB>(predicate);
                    if (source.Count <MyObjectBuilder_FracturedBlock.ShapeB>() <= 0)
                    {
                        child.GetChildren(m_children);
                    }
                    else
                    {
                        MyObjectBuilder_FracturedBlock.ShapeB item = source.First <MyObjectBuilder_FracturedBlock.ShapeB>();
                        Matrix transform = Matrix.CreateFromQuaternion((Quaternion)item.Orientation);
                        transform.Translation = child.GetTransform().Translation;
                        HkdShapeInstanceInfo info2 = new HkdShapeInstanceInfo(child.Shape.Clone(), transform);
                        if (item.Fixed)
                        {
                            info2.Shape.SetFlagRecursively(HkdBreakableShape.Flags.IS_FIXED);
                        }
                        list.Add(info2);
                        m_shapeInfos.Add(info2);
                        this.m_shapes.Remove(item);
                    }
                }
                if (m_shapeInfos.Count == 0)
                {
                    m_children.Clear();
                    throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed.");
                }
                foreach (HkdShapeInstanceInfo info3 in m_shapeInfos)
                {
                    HkdBreakableShape shape = info3.Shape;
                    if (!string.IsNullOrEmpty(shape.Name))
                    {
                        this.Render.AddPiece(info3.Shape.Name, Matrix.CreateFromQuaternion(Quaternion.CreateFromRotationMatrix(info3.GetTransform().GetOrientation())));
                    }
                }
                if (base.CubeGrid.CreatePhysics)
                {
                    HkdBreakableShape?oldParent = null;
                    HkdBreakableShape shape3    = (HkdBreakableShape) new HkdCompoundBreakableShape(oldParent, m_shapeInfos);
                    shape3.RecalcMassPropsFromChildren();
                    this.Shape = shape3;
                    HkMassProperties massProperties = new HkMassProperties();
                    shape3.BuildMassProperties(ref massProperties);
                    this.Shape = new HkdBreakableShape(shape3.GetShape(), ref massProperties);
                    shape3.RemoveReference();
                    foreach (HkdShapeInstanceInfo info4 in m_shapeInfos)
                    {
                        this.Shape.AddShape(ref info4);
                    }
                    this.Shape.SetStrenght(MyDestructionConstants.STRENGTH);
                    this.CreateMountPoints();
                }
                m_children.Clear();
                foreach (HkdShapeInstanceInfo info5 in m_shapeInfos)
                {
                    info5.Shape.RemoveReference();
                }
                foreach (HkdShapeInstanceInfo info6 in list)
                {
                    info6.RemoveReference();
                }
                m_shapeInfos.Clear();
            }
        }
示例#3
0
        public MyObjectBuilder_CubeBlock ConvertToOriginalBlocksWithFractureComponent()
        {
            List <MyObjectBuilder_CubeBlock> blockBuilders = new List <MyObjectBuilder_CubeBlock>();
            Quaternion q;

            for (int i = 0; i < OriginalBlocks.Count; ++i)
            {
                var defId = OriginalBlocks[i];
                MyCubeBlockDefinition def;
                MyDefinitionManager.Static.TryGetCubeBlockDefinition(defId, out def);
                if (def == null)
                {
                    Debug.Fail("Cube block definition not found");
                    continue;
                }
                var orientation = Orientations[i];
                MultiBlockPartInfo multiBlockInfo = MultiBlocks != null && MultiBlocks.Count > i ? MultiBlocks[i] : null;

                MyObjectBuilder_CubeBlock blockBuilder = MyObjectBuilderSerializer.CreateNewObject(defId) as MyObjectBuilder_CubeBlock;
                orientation.GetQuaternion(out q);
                blockBuilder.Orientation          = q;
                blockBuilder.Min                  = Position;
                blockBuilder.MultiBlockId         = multiBlockInfo != null ? multiBlockInfo.MultiBlockId : 0;
                blockBuilder.MultiBlockDefinition = null;
                if (multiBlockInfo != null)
                {
                    blockBuilder.MultiBlockDefinition = multiBlockInfo.MultiBlockDefinition;
                }
                blockBuilder.ComponentContainer = new MyObjectBuilder_ComponentContainer();

                var fractureBuilder         = new MyObjectBuilder_FractureComponentCubeBlock();
                HashSet <string> shapeNames = new HashSet <string>();
                GetAllBlockBreakableShapeNames(def, shapeNames);
                ConvertAllShapesToFractureComponentShapeBuilder(Shape, ref Matrix.Identity, orientation, shapeNames, fractureBuilder);
                // Count of shapes can be 0!
                if (fractureBuilder.Shapes.Count == 0)
                {
                    continue;
                }

                var componentData = new MyObjectBuilder_ComponentContainer.ComponentData();
                componentData.TypeId    = typeof(MyFractureComponentBase).Name;
                componentData.Component = fractureBuilder;
                blockBuilder.ComponentContainer.Components.Add(componentData);

                if (i == 0 && CubeGrid.GridSizeEnum == MyCubeSize.Small)
                {
                    return(blockBuilder);
                }

                blockBuilders.Add(blockBuilder);
            }

            if (blockBuilders.Count > 0)
            {
                MyObjectBuilder_CompoundCubeBlock compoundBuilder = MyCompoundCubeBlock.CreateBuilder(blockBuilders);
                return(compoundBuilder);
            }

            return(null);
        }
示例#4
0
        public MyObjectBuilder_CubeBlock ConvertToOriginalBlocksWithFractureComponent()
        {
            List <MyObjectBuilder_CubeBlock> cubeBlockBuilders = new List <MyObjectBuilder_CubeBlock>();

            for (int i = 0; i < this.OriginalBlocks.Count; i++)
            {
                MyCubeBlockDefinition definition;
                MyDefinitionId        defId = this.OriginalBlocks[i];
                MyDefinitionManager.Static.TryGetCubeBlockDefinition(defId, out definition);
                if (definition != null)
                {
                    Quaternion         quaternion;
                    float              num2;
                    MultiBlockPartInfo local1;
                    MyBlockOrientation blockOrientation = this.Orientations[i];
                    if ((this.MultiBlocks == null) || (this.MultiBlocks.Count <= i))
                    {
                        local1 = null;
                    }
                    else
                    {
                        local1 = this.MultiBlocks[i];
                    }
                    MultiBlockPartInfo        info = local1;
                    MyObjectBuilder_CubeBlock item = MyObjectBuilderSerializer.CreateNewObject((SerializableDefinitionId)defId) as MyObjectBuilder_CubeBlock;
                    blockOrientation.GetQuaternion(out quaternion);
                    item.Orientation          = quaternion;
                    item.Min                  = base.Position;
                    item.MultiBlockId         = (info != null) ? info.MultiBlockId : 0;
                    item.MultiBlockDefinition = null;
                    if (info != null)
                    {
                        item.MultiBlockDefinition = new SerializableDefinitionId?((SerializableDefinitionId)info.MultiBlockDefinition);
                    }
                    item.ComponentContainer = new MyObjectBuilder_ComponentContainer();
                    MyObjectBuilder_FractureComponentCubeBlock fractureComponentBuilder = new MyObjectBuilder_FractureComponentCubeBlock();
                    m_tmpNamesAndBuildProgress.Clear();
                    GetAllBlockBreakableShapeNames(definition, m_tmpNamesAndBuildProgress);
                    ConvertAllShapesToFractureComponentShapeBuilder(this.Shape, ref Matrix.Identity, blockOrientation, m_tmpNamesAndBuildProgress, fractureComponentBuilder, out num2);
                    m_tmpNamesAndBuildProgress.Clear();
                    if (fractureComponentBuilder.Shapes.Count != 0)
                    {
                        if (definition.BuildProgressModels != null)
                        {
                            foreach (MyCubeBlockDefinition.BuildProgressModel model in definition.BuildProgressModels)
                            {
                                if (model.BuildRatioUpperBound >= num2)
                                {
                                    break;
                                }
                                float buildRatioUpperBound = model.BuildRatioUpperBound;
                            }
                        }
                        MyObjectBuilder_ComponentContainer.ComponentData data = new MyObjectBuilder_ComponentContainer.ComponentData {
                            TypeId    = typeof(MyFractureComponentBase).Name,
                            Component = fractureComponentBuilder
                        };
                        item.ComponentContainer.Components.Add(data);
                        item.BuildPercent     = num2;
                        item.IntegrityPercent = MyDefinitionManager.Static.DestructionDefinition.ConvertedFractureIntegrityRatio * num2;
                        if ((i == 0) && (base.CubeGrid.GridSizeEnum == MyCubeSize.Small))
                        {
                            return(item);
                        }
                        cubeBlockBuilders.Add(item);
                    }
                }
            }
            return((cubeBlockBuilders.Count <= 0) ? null : MyCompoundCubeBlock.CreateBuilder(cubeBlockBuilders));
        }
        public MyObjectBuilder_CubeBlock ConvertToOriginalBlocksWithFractureComponent()
        {
            List <MyObjectBuilder_CubeBlock> blockBuilders = new List <MyObjectBuilder_CubeBlock>();
            Quaternion q;

            for (int i = 0; i < OriginalBlocks.Count; ++i)
            {
                var defId = OriginalBlocks[i];
                MyCubeBlockDefinition def;
                MyDefinitionManager.Static.TryGetCubeBlockDefinition(defId, out def);
                if (def == null)
                {
                    Debug.Fail("Cube block definition not found");
                    continue;
                }
                var orientation = Orientations[i];
                MultiBlockPartInfo multiBlockInfo = MultiBlocks != null && MultiBlocks.Count > i ? MultiBlocks[i] : null;

                MyObjectBuilder_CubeBlock blockBuilder = MyObjectBuilderSerializer.CreateNewObject(defId) as MyObjectBuilder_CubeBlock;
                orientation.GetQuaternion(out q);
                blockBuilder.Orientation          = q;
                blockBuilder.Min                  = Position;
                blockBuilder.MultiBlockId         = multiBlockInfo != null ? multiBlockInfo.MultiBlockId : 0;
                blockBuilder.MultiBlockDefinition = null;
                if (multiBlockInfo != null)
                {
                    blockBuilder.MultiBlockDefinition = multiBlockInfo.MultiBlockDefinition;
                }
                blockBuilder.ComponentContainer = new MyObjectBuilder_ComponentContainer();

                var fractureBuilder = new MyObjectBuilder_FractureComponentCubeBlock();
                m_tmpNamesAndBuildProgress.Clear();
                GetAllBlockBreakableShapeNames(def, m_tmpNamesAndBuildProgress);
                float buildProgress;
                ConvertAllShapesToFractureComponentShapeBuilder(Shape, ref Matrix.Identity, orientation, m_tmpNamesAndBuildProgress, fractureBuilder, out buildProgress);
                m_tmpNamesAndBuildProgress.Clear();
                // Count of shapes can be 0!
                if (fractureBuilder.Shapes.Count == 0)
                {
                    continue;
                }

                float previousBuildRatioUpperBound = 0f;
                if (def.BuildProgressModels != null)
                {
                    foreach (var progress in def.BuildProgressModels)
                    {
                        if (progress.BuildRatioUpperBound >= buildProgress)
                        {
                            break;
                        }

                        previousBuildRatioUpperBound = progress.BuildRatioUpperBound;
                    }
                }

                var componentData = new MyObjectBuilder_ComponentContainer.ComponentData();
                componentData.TypeId    = typeof(MyFractureComponentBase).Name;
                componentData.Component = fractureBuilder;
                blockBuilder.ComponentContainer.Components.Add(componentData);
                blockBuilder.BuildPercent = buildProgress;
                Debug.Assert(buildProgress > previousBuildRatioUpperBound);
                blockBuilder.IntegrityPercent = MyDefinitionManager.Static.DestructionDefinition.ConvertedFractureIntegrityRatio * buildProgress;

                if (i == 0 && CubeGrid.GridSizeEnum == MyCubeSize.Small)
                {
                    return(blockBuilder);
                }

                blockBuilders.Add(blockBuilder);
            }

            if (blockBuilders.Count > 0)
            {
                MyObjectBuilder_CompoundCubeBlock compoundBuilder = MyCompoundCubeBlock.CreateBuilder(blockBuilders);
                return(compoundBuilder);
            }

            return(null);
        }