/// <summary>
        /// Check if other block can be added to area of multiblock.
        /// </summary>
        public bool CanAddBlock(ref Vector3I otherGridPositionMin, ref Vector3I otherGridPositionMax, MyBlockOrientation otherOrientation, MyCubeBlockDefinition otherDefinition)
        {
            MatrixI transform;

            if (!GetTransform(out transform))
            {
                return(true);
            }

            try
            {
                // Calculate other block position in multiblock space.
                MatrixI invTransform;
                MatrixI.Invert(ref transform, out invTransform);

                Vector3I otherPositionInMultiBlockMinTmp = Vector3I.Transform(otherGridPositionMin, ref invTransform);
                Vector3I otherPositionInMultiBlockMaxTmp = Vector3I.Transform(otherGridPositionMax, ref invTransform);
                Vector3I otherPositionInMultiBlockMin    = Vector3I.Min(otherPositionInMultiBlockMinTmp, otherPositionInMultiBlockMaxTmp);
                Vector3I otherPositionInMultiBlockMax    = Vector3I.Max(otherPositionInMultiBlockMinTmp, otherPositionInMultiBlockMaxTmp);

                // Check intersection with AABB of whole multiblock
                if (!Vector3I.BoxIntersects(ref MultiBlockDefinition.Min, ref MultiBlockDefinition.Max, ref otherPositionInMultiBlockMin, ref otherPositionInMultiBlockMax))
                {
                    return(true);
                }

                // Other block rotation in multiblock space.
                MatrixI otherRotation = new MatrixI(otherOrientation);
                MatrixI otherRotationInMultiBlock;
                MatrixI.Multiply(ref otherRotation, ref invTransform, out otherRotationInMultiBlock);
                MyBlockOrientation otherOrientationInMultiBlock = new MyBlockOrientation(otherRotationInMultiBlock.Forward, otherRotationInMultiBlock.Up);

                // Multiblock part (block) definitions in the same position.
                m_tmpPartDefinitions.Clear();
                foreach (var partDefinition in MultiBlockDefinition.BlockDefinitions)
                {
                    if (Vector3I.BoxIntersects(ref partDefinition.Min, ref partDefinition.Max, ref otherPositionInMultiBlockMin, ref otherPositionInMultiBlockMax))
                    {
                        if (otherPositionInMultiBlockMin == otherPositionInMultiBlockMax && partDefinition.Min == partDefinition.Max) // Size = 1
                        {
                            m_tmpPartDefinitions.Add(partDefinition);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }

                if (m_tmpPartDefinitions.Count == 0)
                {
                    return(true);
                }

                // Check if multiblock part blocks and other block can be added together
                bool canAdd = true;
                foreach (var partDefinition in m_tmpPartDefinitions)
                {
                    MyCubeBlockDefinition blockDefinition;
                    if (MyDefinitionManager.Static.TryGetCubeBlockDefinition(partDefinition.Id, out blockDefinition) && blockDefinition != null)
                    {
                        canAdd &= MyCompoundCubeBlock.CanAddBlocks(blockDefinition, new MyBlockOrientation(partDefinition.Forward, partDefinition.Up),
                                                                   otherDefinition, otherOrientationInMultiBlock);
                        if (!canAdd)
                        {
                            break;
                        }
                    }
                }

                return(canAdd);
            }
            finally
            {
                m_tmpPartDefinitions.Clear();
            }
        }
Example #2
0
        public bool CanAddBlock(ref Vector3I otherGridPositionMin, ref Vector3I otherGridPositionMax, MyBlockOrientation otherOrientation, MyCubeBlockDefinition otherDefinition)
        {
            MatrixI xi;
            bool    flag2;

            if (!this.GetTransform(out xi))
            {
                return(true);
            }
            try
            {
                MatrixI xi2;
                MatrixI.Invert(ref xi, out xi2);
                Vector3I vectori1 = Vector3I.Transform(otherGridPositionMin, ref xi2);
                Vector3I vectori  = Vector3I.Transform(otherGridPositionMax, ref xi2);
                Vector3I minB     = Vector3I.Min(vectori1, vectori);
                Vector3I maxB     = Vector3I.Max(vectori1, vectori);
                if (Vector3I.BoxIntersects(ref this.MultiBlockDefinition.Min, ref this.MultiBlockDefinition.Max, ref minB, ref maxB))
                {
                    MatrixI xi4;
                    MatrixI leftMatrix = new MatrixI(otherOrientation);
                    MatrixI.Multiply(ref leftMatrix, ref xi2, out xi4);
                    MyBlockOrientation orientation = new MyBlockOrientation(xi4.Forward, xi4.Up);
                    m_tmpPartDefinitions.Clear();
                    MyMultiBlockDefinition.MyMultiBlockPartDefinition[] blockDefinitions = this.MultiBlockDefinition.BlockDefinitions;
                    int index = 0;
                    while (true)
                    {
                        if (index < blockDefinitions.Length)
                        {
                            MyMultiBlockDefinition.MyMultiBlockPartDefinition item = blockDefinitions[index];
                            if (Vector3I.BoxIntersects(ref item.Min, ref item.Max, ref minB, ref maxB))
                            {
                                if (!(minB == maxB))
                                {
                                    break;
                                }
                                if (!(item.Min == item.Max))
                                {
                                    break;
                                }
                                m_tmpPartDefinitions.Add(item);
                            }
                            index++;
                            continue;
                        }
                        if (m_tmpPartDefinitions.Count == 0)
                        {
                            flag2 = true;
                        }
                        else
                        {
                            bool flag = true;
                            foreach (MyMultiBlockDefinition.MyMultiBlockPartDefinition definition2 in m_tmpPartDefinitions)
                            {
                                MyCubeBlockDefinition definition3;
                                if (!MyDefinitionManager.Static.TryGetCubeBlockDefinition(definition2.Id, out definition3))
                                {
                                    continue;
                                }
                                if (definition3 != null)
                                {
                                    flag &= MyCompoundCubeBlock.CanAddBlocks(definition3, new MyBlockOrientation(definition2.Forward, definition2.Up), otherDefinition, orientation);
                                    if (!flag)
                                    {
                                        break;
                                    }
                                }
                            }
                            flag2 = flag;
                        }
                        return(flag2);
                    }
                    flag2 = false;
                }
                else
                {
                    flag2 = true;
                }
            }
            finally
            {
                m_tmpPartDefinitions.Clear();
            }
            return(flag2);
        }