public MyObjectBuilder_ComponentContainer Serialize(bool copy = false) { var tmpSerializedComponents = new List <KeyValuePair <Type, MyComponentBase> >(8); tmpSerializedComponents.Clear(); foreach (var component in m_components) { if (component.Value.IsSerialized()) { tmpSerializedComponents.Add(component); } } if (tmpSerializedComponents.Count == 0) { return(null); } var builder = new MyObjectBuilder_ComponentContainer(); foreach (var component in tmpSerializedComponents) { MyObjectBuilder_ComponentBase componentBuilder = component.Value.Serialize(copy); if (componentBuilder != null) { var data = new MyObjectBuilder_ComponentContainer.ComponentData(); data.TypeId = component.Key.Name; data.Component = componentBuilder; builder.Components.Add(data); } } tmpSerializedComponents.Clear(); return(builder); }
public MyObjectBuilder_ComponentContainer Serialize() { var builder = new MyObjectBuilder_ComponentContainer(); var componentsData = new List <MyObjectBuilder_ComponentContainer.ComponentData>(m_components.Count); int i = 0; foreach (var component in m_components) { MyObjectBuilder_ComponentBase componentBuilder = null; if (component.Value.IsSerialized()) { componentBuilder = component.Value.Serialize(); } if (componentBuilder != null) { var data = new MyObjectBuilder_ComponentContainer.ComponentData(); data.TypeId = component.Key.Name; data.Component = componentBuilder; componentsData.Add(data); i++; } } if (componentsData.Count > 0) { builder.Components = componentsData.ToArray(); } return(builder); }
public static MyObjectBuilder_ComponentBase FindComponentBuilder(MyContainerDefinition.DefaultComponent component, MyObjectBuilder_ComponentContainer builder) { MyObjectBuilder_ComponentBase base2 = null; if ((builder != null) && component.IsValid()) { null; if (!component.BuilderType.IsNull) { MyObjectBuilder_ComponentContainer.ComponentData data = builder.Components.Find(x => x.Component.TypeId == component.BuilderType); if (data != null) { base2 = data.Component; } } } return(base2); }
/// <summary> /// Removes all sign of a pilot/characrter from a cockpit cube. /// </summary> /// <param name="cockpit">The specific cube.</param> /// <param name="character">Specific character to remove, if required, otherwise ANY chararcter will be removed.</param> /// <returns>Returns true if a character was removed.</returns> public static bool RemoveHierarchyCharacter(this MyObjectBuilder_Cockpit cockpit, MyObjectBuilder_Character character = null) { bool retValue = false; MyObjectBuilder_ComponentContainer.ComponentData hierarchyComponentBase = cockpit.ComponentContainer?.Components?.FirstOrDefault(e => e.TypeId == "MyHierarchyComponentBase"); var hierarchyBase = hierarchyComponentBase?.Component as MyObjectBuilder_HierarchyComponentBase; if (hierarchyBase != null && hierarchyBase.Children.Count > 0) { for (int i = 0; i < hierarchyBase.Children.Count; i++) { if (character != null && hierarchyBase.Children[i] == character) { retValue = true; hierarchyBase.Children.RemoveAt(i); i--; break; } if (character == null && hierarchyBase.Children[i] is MyObjectBuilder_Character) { retValue = true; hierarchyBase.Children.RemoveAt(i); i--; } } if (hierarchyBase.Children.Count == 0) { cockpit.ComponentContainer.Components.Remove(hierarchyComponentBase); } } if (retValue) { cockpit.ClearPilotAndAutopilot(); cockpit.PilotRelativeWorld = null; // This should also clear Pilot. cockpit.Pilot = null; } return(retValue); }
// This function does some modifications to the cube block's object builder before it's built, usually integrity changes, etc... public virtual void BeforeCreateBlock(MyCubeBlockDefinition definition, MyEntity builder, MyObjectBuilder_CubeBlock ob, bool buildAsAdmin) { if (definition.EntityComponents == null) { return; } if (ob.ComponentContainer == null) { ob.ComponentContainer = new MyObjectBuilder_ComponentContainer(); } foreach (var componentOb in definition.EntityComponents) { var data = new MyObjectBuilder_ComponentContainer.ComponentData(); data.TypeId = componentOb.Key.ToString(); data.Component = componentOb.Value; ob.ComponentContainer.Components.Add(data); } }
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(); 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; }
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); }
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); }