public virtual void Init(MyContainerDefinition definition)
 {
 }
Example #2
0
 public static void InitComponents(this MyComponentContainer container, MyObjectBuilderType type, MyStringHash subtypeName, MyObjectBuilder_ComponentContainer builder)
 {
     if (MyDefinitionManager.Static != null)
     {
         MyContainerDefinition definition = null;
         bool flag = ReferenceEquals(builder, null);
         if (TryGetContainerDefinition(type, subtypeName, out definition))
         {
             container.Init(definition);
             if (definition.DefaultComponents != null)
             {
                 foreach (MyContainerDefinition.DefaultComponent component in definition.DefaultComponents)
                 {
                     MyComponentDefinitionBase     componentDefinition = null;
                     MyObjectBuilder_ComponentBase base3 = FindComponentBuilder(component, builder);
                     bool            flag2 = base3 != null;
                     Type            type2 = null;
                     MyComponentBase base4 = null;
                     MyStringHash    hash  = subtypeName;
                     if (component.SubtypeId != null)
                     {
                         hash = component.SubtypeId.Value;
                     }
                     if (TryGetComponentDefinition(component.BuilderType, hash, out componentDefinition))
                     {
                         base4 = MyComponentFactory.CreateInstanceByTypeId(componentDefinition.Id.TypeId);
                         base4.Init(componentDefinition);
                     }
                     else if (component.IsValid())
                     {
                         base4 = component.BuilderType.IsNull ? MyComponentFactory.CreateInstanceByType(component.InstanceType) : MyComponentFactory.CreateInstanceByTypeId(component.BuilderType);
                     }
                     if (base4 != null)
                     {
                         Type componentType = MyComponentTypeFactory.GetComponentType(base4.GetType());
                         if (componentType != null)
                         {
                             type2 = componentType;
                         }
                         else
                         {
                             MyComponentDefinitionBase base1 = componentDefinition;
                         }
                     }
                     if ((type2 == null) && (base4 != null))
                     {
                         type2 = base4.GetType();
                     }
                     if (((base4 != null) && (type2 != null)) && ((flag | flag2) || component.ForceCreate))
                     {
                         if (base3 != null)
                         {
                             base4.Deserialize(base3);
                         }
                         container.Add(type2, base4);
                     }
                 }
             }
         }
         container.Deserialize(builder);
     }
 }
 public override void Init(MyContainerDefinition definition)
 {
     if (definition.Flags != null)
         Entity.Flags |= definition.Flags.Value;
 }
Example #4
0
        public static bool TryGetContainerDefinition(MyObjectBuilderType type, MyStringHash subtypeName, out MyContainerDefinition definition)
        {
            definition = null;
            if (MyDefinitionManager.Static == null)
            {
                return(false);
            }
            MyDefinitionId containerId = new MyDefinitionId(type, subtypeName);

            if (MyDefinitionManager.Static.TryGetContainerDefinition(containerId, out definition))
            {
                return(true);
            }
            if (subtypeName != MyStringHash.NullOrEmpty)
            {
                MyDefinitionId id3 = new MyDefinitionId(typeof(MyObjectBuilder_EntityBase), subtypeName);
                if (MyDefinitionManager.Static.TryGetContainerDefinition(id3, out definition))
                {
                    return(true);
                }
            }
            MyDefinitionId id2 = new MyDefinitionId(type);

            return(MyDefinitionManager.Static.TryGetContainerDefinition(id2, out definition));
        }
Example #5
0
        /// <summary>
        /// Spawns bag around position given by "baseTransform", checks all 4 directions around - forwards (forward, right, backward, left) and on each such direction moves test sphere
        /// in 3 directions forward (frontChecks), sides (perpendicular to forward direction - rights) and up. If spawn position is not found then position above "worldAabbTopPosition"
        /// is selected.
        /// </summary>
        private static MyEntity SpawnBagAround(MyEntity itemOwner, MyContainerDefinition bagDefinition,
                                               int sideCheckCount = 3, int frontCheckCount = 2, int upCheckCount = 5, float stepSize = 1f)
        {
            Debug.Assert(Sandbox.Game.Multiplayer.Sync.IsServer);

            Vector3D?finalPos = null;

            // Model sphere
            MyModel bagModel = null;

            foreach (var componentDef in bagDefinition.DefaultComponents)
            {
                if (typeof(MyObjectBuilder_ModelComponent).IsAssignableFrom(componentDef.BuilderType))
                {
                    MyComponentDefinitionBase componentDefinition = null;
                    var componentSubtype = bagDefinition.Id.SubtypeId;
                    if (componentDef.SubtypeId.HasValue)
                    {
                        componentSubtype = componentDef.SubtypeId.Value;
                    }

                    if (MyComponentContainerExtension.TryGetComponentDefinition(componentDef.BuilderType, componentSubtype, out componentDefinition))
                    {
                        var modelComponentDef = componentDefinition as MyModelComponentDefinition;
                        Debug.Assert(modelComponentDef != null);
                        if (modelComponentDef != null)
                        {
                            bagModel = MyModels.GetModelOnlyData(modelComponentDef.Model);
                        }
                    }

                    break;
                }
            }

            Debug.Assert(bagModel != null);
            if (bagModel == null)
            {
                return(null);
            }

            float   bagBoxRadius = bagModel.BoundingBox.HalfExtents.Max();
            HkShape sphere       = new HkSphereShape(bagBoxRadius);

            try
            {
                Vector3D basePos = itemOwner.PositionComp.WorldMatrix.Translation;
                float    step    = bagBoxRadius * stepSize;

                // Calculate right, up and forward vectors from gravity
                Vector3 upDir = -MyGravityProviderSystem.CalculateNaturalGravityInPoint(itemOwner.PositionComp.WorldMatrix.Translation);
                if (upDir == Vector3.Zero)
                {
                    upDir = Vector3.Up;
                }
                else
                {
                    upDir.Normalize();
                }

                Vector3 forwardDir;
                upDir.CalculatePerpendicularVector(out forwardDir);

                Vector3 rightDir = Vector3.Cross(forwardDir, upDir);
                rightDir.Normalize();

                Vector3D   currentPos;
                Quaternion rot = Quaternion.Identity;

                Vector3[] forwards = new Vector3[]
                {
                    forwardDir,
                    rightDir,
                    -forwardDir,
                    -rightDir
                };

                Vector3[] rights = new Vector3[]
                {
                    rightDir,
                    -forwardDir,
                    -rightDir,
                    forwardDir
                };

                // All sides
                for (int i = 0; i < forwards.Length && finalPos == null; ++i)
                {
                    var forward = forwards[i];
                    var right   = rights[i];

                    // Move forward
                    for (int frontMove = 0; frontMove < frontCheckCount && finalPos == null; ++frontMove)
                    {
                        Vector3D sidePosBase = basePos + 0.25f * forward + bagBoxRadius * forward + frontMove * step * forward - 0.5f * (sideCheckCount - 1) * step * right;

                        // Move perp to forward
                        for (int sideMove = 0; sideMove < sideCheckCount && finalPos == null; ++sideMove)
                        {
                            // Move up
                            for (int upMove = 0; upMove < upCheckCount && finalPos == null; ++upMove)
                            {
                                currentPos = sidePosBase + sideMove * step * right + upMove * step * upDir;

                                if (MyEntities.IsInsideWorld(currentPos) && !MyEntities.IsShapePenetrating(sphere, ref currentPos, ref rot))
                                {
                                    BoundingSphereD boundingSphere     = new BoundingSphereD(currentPos, bagBoxRadius);
                                    MyVoxelBase     overlappedVoxelmap = MySession.Static.VoxelMaps.GetOverlappingWithSphere(ref boundingSphere);

                                    if (overlappedVoxelmap == null)
                                    {
                                        finalPos = currentPos;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                // If not found position then select position above aabb's top.
                if (finalPos == null)
                {
                    MyOrientedBoundingBoxD obb     = new MyOrientedBoundingBoxD((BoundingBoxD)itemOwner.PositionComp.LocalAABB, itemOwner.PositionComp.WorldMatrix);
                    Vector3D[]             corners = new Vector3D[8];
                    obb.GetCorners(corners, 0);
                    float dotUp = float.MinValue;
                    foreach (var corner in corners)
                    {
                        var localDot = Vector3.Dot(corner - obb.Center, upDir);
                        dotUp = Math.Max(dotUp, localDot);
                    }

                    finalPos = itemOwner.PositionComp.WorldMatrix.Translation;
                    Debug.Assert(dotUp > 0);
                    if (dotUp > 0)
                    {
                        finalPos = obb.Center + dotUp * upDir;
                    }
                }
            }
            finally
            {
                sphere.RemoveReference();
            }

            Debug.Assert(finalPos != null);

            MatrixD transform = itemOwner.PositionComp.WorldMatrix;

            transform.Translation = finalPos.Value;

            MyEntity bagEntity = MyEntities.CreateFromComponentContainerDefinitionAndAdd(bagDefinition.Id);

            if (bagEntity == null)
            {
                return(null);
            }

            bagEntity.PositionComp.SetWorldMatrix(transform);

            bagEntity.Physics.LinearVelocity  = Vector3.Zero;
            bagEntity.Physics.AngularVelocity = Vector3.Zero;

            return(bagEntity);
        }