Пример #1
0
        public static bool CanPlace(int x, int y, int type, int style, int dir, out TileObject objectData, bool onlyCheck = false)
        {
            TileObjectData tileData = TileObjectData.GetTileData(type, style);

            objectData = Empty;
            if (tileData == null)
            {
                return(false);
            }
            int num  = x - tileData.Origin.X;
            int num2 = y - tileData.Origin.Y;

            if (num < 0 || num + tileData.Width >= Main.maxTilesX || num2 < 0 || num2 + tileData.Height >= Main.maxTilesY)
            {
                return(false);
            }
            bool flag = tileData.RandomStyleRange > 0;

            if (TileObjectPreviewData.placementCache == null)
            {
                TileObjectPreviewData.placementCache = new TileObjectPreviewData();
            }
            TileObjectPreviewData.placementCache.Reset();
            int num3 = 0;

            if (tileData.AlternatesCount != 0)
            {
                num3 = tileData.AlternatesCount;
            }
            float          num4           = -1f;
            float          num5           = -1f;
            int            num6           = 0;
            TileObjectData tileObjectData = null;
            int            num7           = -1;

            while (num7 < num3)
            {
                num7++;
                TileObjectData tileData2 = TileObjectData.GetTileData(type, style, num7);
                if (tileData2.Direction != 0 && ((tileData2.Direction == TileObjectDirection.PlaceLeft && dir == 1) || (tileData2.Direction == TileObjectDirection.PlaceRight && dir == -1)))
                {
                    continue;
                }
                int num8 = x - tileData2.Origin.X;
                int num9 = y - tileData2.Origin.Y;
                if (num8 < 5 || num8 + tileData2.Width > Main.maxTilesX - 5 || num9 < 5 || num9 + tileData2.Height > Main.maxTilesY - 5)
                {
                    return(false);
                }
                Rectangle rectangle = new Rectangle(0, 0, tileData2.Width, tileData2.Height);
                int       num10     = 0;
                int       num11     = 0;
                if (tileData2.AnchorTop.tileCount != 0)
                {
                    if (rectangle.Y == 0)
                    {
                        rectangle.Y = -1;
                        rectangle.Height++;
                        num11++;
                    }
                    int checkStart = tileData2.AnchorTop.checkStart;
                    if (checkStart < rectangle.X)
                    {
                        rectangle.Width += rectangle.X - checkStart;
                        num10           += rectangle.X - checkStart;
                        rectangle.X      = checkStart;
                    }
                    int num12 = checkStart + tileData2.AnchorTop.tileCount - 1;
                    int num13 = rectangle.X + rectangle.Width - 1;
                    if (num12 > num13)
                    {
                        rectangle.Width += num12 - num13;
                    }
                }
                if (tileData2.AnchorBottom.tileCount != 0)
                {
                    if (rectangle.Y + rectangle.Height == tileData2.Height)
                    {
                        rectangle.Height++;
                    }
                    int checkStart2 = tileData2.AnchorBottom.checkStart;
                    if (checkStart2 < rectangle.X)
                    {
                        rectangle.Width += rectangle.X - checkStart2;
                        num10           += rectangle.X - checkStart2;
                        rectangle.X      = checkStart2;
                    }
                    int num14 = checkStart2 + tileData2.AnchorBottom.tileCount - 1;
                    int num15 = rectangle.X + rectangle.Width - 1;
                    if (num14 > num15)
                    {
                        rectangle.Width += num14 - num15;
                    }
                }
                if (tileData2.AnchorLeft.tileCount != 0)
                {
                    if (rectangle.X == 0)
                    {
                        rectangle.X = -1;
                        rectangle.Width++;
                        num10++;
                    }
                    int num16 = tileData2.AnchorLeft.checkStart;
                    if ((tileData2.AnchorLeft.type & AnchorType.Tree) == AnchorType.Tree)
                    {
                        num16--;
                    }
                    if (num16 < rectangle.Y)
                    {
                        rectangle.Width += rectangle.Y - num16;
                        num11           += rectangle.Y - num16;
                        rectangle.Y      = num16;
                    }
                    int num17 = num16 + tileData2.AnchorLeft.tileCount - 1;
                    if ((tileData2.AnchorLeft.type & AnchorType.Tree) == AnchorType.Tree)
                    {
                        num17 += 2;
                    }
                    int num18 = rectangle.Y + rectangle.Height - 1;
                    if (num17 > num18)
                    {
                        rectangle.Height += num17 - num18;
                    }
                }
                if (tileData2.AnchorRight.tileCount != 0)
                {
                    if (rectangle.X + rectangle.Width == tileData2.Width)
                    {
                        rectangle.Width++;
                    }
                    int num19 = tileData2.AnchorLeft.checkStart;
                    if ((tileData2.AnchorRight.type & AnchorType.Tree) == AnchorType.Tree)
                    {
                        num19--;
                    }
                    if (num19 < rectangle.Y)
                    {
                        rectangle.Width += rectangle.Y - num19;
                        num11           += rectangle.Y - num19;
                        rectangle.Y      = num19;
                    }
                    int num20 = num19 + tileData2.AnchorRight.tileCount - 1;
                    if ((tileData2.AnchorRight.type & AnchorType.Tree) == AnchorType.Tree)
                    {
                        num20 += 2;
                    }
                    int num21 = rectangle.Y + rectangle.Height - 1;
                    if (num20 > num21)
                    {
                        rectangle.Height += num20 - num21;
                    }
                }
                if (onlyCheck)
                {
                    objectPreview.Reset();
                    objectPreview.Active      = true;
                    objectPreview.Type        = (ushort)type;
                    objectPreview.Style       = (short)style;
                    objectPreview.Alternate   = num7;
                    objectPreview.Size        = new Point16(rectangle.Width, rectangle.Height);
                    objectPreview.ObjectStart = new Point16(num10, num11);
                    objectPreview.Coordinates = new Point16(num8 - num10, num9 - num11);
                }
                float num22 = 0f;
                float num23 = tileData2.Width * tileData2.Height;
                float num24 = 0f;
                float num25 = 0f;
                for (int i = 0; i < tileData2.Width; i++)
                {
                    for (int j = 0; j < tileData2.Height; j++)
                    {
                        Tile tileSafely = Framing.GetTileSafely(num8 + i, num9 + j);
                        bool flag2      = !tileData2.LiquidPlace(tileSafely);
                        bool flag3      = false;
                        if (tileData2.AnchorWall)
                        {
                            num25 += 1f;
                            if (!tileData2.isValidWallAnchor(tileSafely.wall))
                            {
                                flag3 = true;
                            }
                            else
                            {
                                num24 += 1f;
                            }
                        }
                        bool flag4 = false;
                        if (tileSafely.active() && (!Main.tileCut[tileSafely.type] || tileSafely.type == 484) && !TileID.Sets.BreakableWhenPlacing[tileSafely.type])
                        {
                            flag4 = true;
                        }
                        if (flag4 | flag2 | flag3)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[i + num10, j + num11] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[i + num10, j + num11] = 1;
                        }
                        num22 += 1f;
                    }
                }
                AnchorData anchorBottom = tileData2.AnchorBottom;
                if (anchorBottom.tileCount != 0)
                {
                    num25 += (float)anchorBottom.tileCount;
                    int height = tileData2.Height;
                    for (int k = 0; k < anchorBottom.tileCount; k++)
                    {
                        int  num26      = anchorBottom.checkStart + k;
                        Tile tileSafely = Framing.GetTileSafely(num8 + num26, num9 + height);
                        bool flag5      = false;
                        if (tileSafely.nactive())
                        {
                            if ((anchorBottom.type & AnchorType.SolidTile) == AnchorType.SolidTile && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type] && !Main.tileNoAttach[tileSafely.type] && (tileData2.FlattenAnchors || tileSafely.blockType() == 0))
                            {
                                flag5 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag5 && ((anchorBottom.type & AnchorType.SolidWithTop) == AnchorType.SolidWithTop || (anchorBottom.type & AnchorType.Table) == AnchorType.Table))
                            {
                                if (TileID.Sets.Platforms[tileSafely.type])
                                {
                                    _ = tileSafely.frameX / TileObjectData.PlatformFrameWidth();
                                    if (!tileSafely.halfBrick() && WorldGen.PlatformProperTopFrame(tileSafely.frameX))
                                    {
                                        flag5 = true;
                                    }
                                }
                                else if (Main.tileSolid[tileSafely.type] && Main.tileSolidTop[tileSafely.type])
                                {
                                    flag5 = true;
                                }
                            }
                            if (!flag5 && (anchorBottom.type & AnchorType.Table) == AnchorType.Table && !TileID.Sets.Platforms[tileSafely.type] && Main.tileTable[tileSafely.type] && tileSafely.blockType() == 0)
                            {
                                flag5 = true;
                            }
                            if (!flag5 && (anchorBottom.type & AnchorType.SolidSide) == AnchorType.SolidSide && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type])
                            {
                                int num27 = tileSafely.blockType();
                                if ((uint)(num27 - 4) <= 1u)
                                {
                                    flag5 = tileData2.isValidTileAnchor(tileSafely.type);
                                }
                            }
                            if (!flag5 && (anchorBottom.type & AnchorType.AlternateTile) == AnchorType.AlternateTile && tileData2.isValidAlternateAnchor(tileSafely.type))
                            {
                                flag5 = true;
                            }
                        }
                        else if (!flag5 && (anchorBottom.type & AnchorType.EmptyTile) == AnchorType.EmptyTile)
                        {
                            flag5 = true;
                        }
                        if (!flag5)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[num26 + num10, height + num11] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[num26 + num10, height + num11] = 1;
                        }
                        num24 += 1f;
                    }
                }
                anchorBottom = tileData2.AnchorTop;
                if (anchorBottom.tileCount != 0)
                {
                    num25 += (float)anchorBottom.tileCount;
                    int num28 = -1;
                    for (int l = 0; l < anchorBottom.tileCount; l++)
                    {
                        int  num29      = anchorBottom.checkStart + l;
                        Tile tileSafely = Framing.GetTileSafely(num8 + num29, num9 + num28);
                        bool flag6      = false;
                        if (tileSafely.nactive())
                        {
                            if (Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type] && !Main.tileNoAttach[tileSafely.type] && (tileData2.FlattenAnchors || tileSafely.blockType() == 0))
                            {
                                flag6 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag6 && (anchorBottom.type & AnchorType.SolidBottom) == AnchorType.SolidBottom && ((Main.tileSolid[tileSafely.type] && (!Main.tileSolidTop[tileSafely.type] || (TileID.Sets.Platforms[tileSafely.type] && (tileSafely.halfBrick() || tileSafely.topSlope())))) || tileSafely.halfBrick() || tileSafely.topSlope()) && !TileID.Sets.NotReallySolid[tileSafely.type] && !tileSafely.bottomSlope())
                            {
                                flag6 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag6 && (anchorBottom.type & AnchorType.SolidSide) == AnchorType.SolidSide && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type])
                            {
                                int num27 = tileSafely.blockType();
                                if ((uint)(num27 - 2) <= 1u)
                                {
                                    flag6 = tileData2.isValidTileAnchor(tileSafely.type);
                                }
                            }
                            if (!flag6 && (anchorBottom.type & AnchorType.AlternateTile) == AnchorType.AlternateTile && tileData2.isValidAlternateAnchor(tileSafely.type))
                            {
                                flag6 = true;
                            }
                        }
                        else if (!flag6 && (anchorBottom.type & AnchorType.EmptyTile) == AnchorType.EmptyTile)
                        {
                            flag6 = true;
                        }
                        if (!flag6)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[num29 + num10, num28 + num11] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[num29 + num10, num28 + num11] = 1;
                        }
                        num24 += 1f;
                    }
                }
                anchorBottom = tileData2.AnchorRight;
                if (anchorBottom.tileCount != 0)
                {
                    num25 += (float)anchorBottom.tileCount;
                    int width = tileData2.Width;
                    for (int m = 0; m < anchorBottom.tileCount; m++)
                    {
                        int  num30      = anchorBottom.checkStart + m;
                        Tile tileSafely = Framing.GetTileSafely(num8 + width, num9 + num30);
                        bool flag7      = false;
                        if (tileSafely.nactive())
                        {
                            if (Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type] && !Main.tileNoAttach[tileSafely.type] && (tileData2.FlattenAnchors || tileSafely.blockType() == 0))
                            {
                                flag7 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag7 && (anchorBottom.type & AnchorType.SolidSide) == AnchorType.SolidSide && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type])
                            {
                                int num27 = tileSafely.blockType();
                                if (num27 == 2 || num27 == 4)
                                {
                                    flag7 = tileData2.isValidTileAnchor(tileSafely.type);
                                }
                            }
                            if (!flag7 && (anchorBottom.type & AnchorType.Tree) == AnchorType.Tree && TileID.Sets.IsATreeTrunk[tileSafely.type])
                            {
                                flag7 = true;
                                if (m == 0)
                                {
                                    num25 += 1f;
                                    Tile tileSafely2 = Framing.GetTileSafely(num8 + width, num9 + num30 - 1);
                                    if (tileSafely2.nactive() && TileID.Sets.IsATreeTrunk[tileSafely2.type])
                                    {
                                        num24 += 1f;
                                        if (onlyCheck)
                                        {
                                            objectPreview[width + num10, num30 + num11 - 1] = 1;
                                        }
                                    }
                                    else if (onlyCheck)
                                    {
                                        objectPreview[width + num10, num30 + num11 - 1] = 2;
                                    }
                                }
                                if (m == anchorBottom.tileCount - 1)
                                {
                                    num25 += 1f;
                                    Tile tileSafely3 = Framing.GetTileSafely(num8 + width, num9 + num30 + 1);
                                    if (tileSafely3.nactive() && TileID.Sets.IsATreeTrunk[tileSafely3.type])
                                    {
                                        num24 += 1f;
                                        if (onlyCheck)
                                        {
                                            objectPreview[width + num10, num30 + num11 + 1] = 1;
                                        }
                                    }
                                    else if (onlyCheck)
                                    {
                                        objectPreview[width + num10, num30 + num11 + 1] = 2;
                                    }
                                }
                            }
                            if (!flag7 && (anchorBottom.type & AnchorType.AlternateTile) == AnchorType.AlternateTile && tileData2.isValidAlternateAnchor(tileSafely.type))
                            {
                                flag7 = true;
                            }
                        }
                        else if (!flag7 && (anchorBottom.type & AnchorType.EmptyTile) == AnchorType.EmptyTile)
                        {
                            flag7 = true;
                        }
                        if (!flag7)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[width + num10, num30 + num11] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[width + num10, num30 + num11] = 1;
                        }
                        num24 += 1f;
                    }
                }
                anchorBottom = tileData2.AnchorLeft;
                if (anchorBottom.tileCount != 0)
                {
                    num25 += (float)anchorBottom.tileCount;
                    int num31 = -1;
                    for (int n = 0; n < anchorBottom.tileCount; n++)
                    {
                        int  num32      = anchorBottom.checkStart + n;
                        Tile tileSafely = Framing.GetTileSafely(num8 + num31, num9 + num32);
                        bool flag8      = false;
                        if (tileSafely.nactive())
                        {
                            if (Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type] && !Main.tileNoAttach[tileSafely.type] && (tileData2.FlattenAnchors || tileSafely.blockType() == 0))
                            {
                                flag8 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag8 && (anchorBottom.type & AnchorType.SolidSide) == AnchorType.SolidSide && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type])
                            {
                                int num27 = tileSafely.blockType();
                                if (num27 == 3 || num27 == 5)
                                {
                                    flag8 = tileData2.isValidTileAnchor(tileSafely.type);
                                }
                            }
                            if (!flag8 && (anchorBottom.type & AnchorType.Tree) == AnchorType.Tree && TileID.Sets.IsATreeTrunk[tileSafely.type])
                            {
                                flag8 = true;
                                if (n == 0)
                                {
                                    num25 += 1f;
                                    Tile tileSafely4 = Framing.GetTileSafely(num8 + num31, num9 + num32 - 1);
                                    if (tileSafely4.nactive() && TileID.Sets.IsATreeTrunk[tileSafely4.type])
                                    {
                                        num24 += 1f;
                                        if (onlyCheck)
                                        {
                                            objectPreview[num31 + num10, num32 + num11 - 1] = 1;
                                        }
                                    }
                                    else if (onlyCheck)
                                    {
                                        objectPreview[num31 + num10, num32 + num11 - 1] = 2;
                                    }
                                }
                                if (n == anchorBottom.tileCount - 1)
                                {
                                    num25 += 1f;
                                    Tile tileSafely5 = Framing.GetTileSafely(num8 + num31, num9 + num32 + 1);
                                    if (tileSafely5.nactive() && TileID.Sets.IsATreeTrunk[tileSafely5.type])
                                    {
                                        num24 += 1f;
                                        if (onlyCheck)
                                        {
                                            objectPreview[num31 + num10, num32 + num11 + 1] = 1;
                                        }
                                    }
                                    else if (onlyCheck)
                                    {
                                        objectPreview[num31 + num10, num32 + num11 + 1] = 2;
                                    }
                                }
                            }
                            if (!flag8 && (anchorBottom.type & AnchorType.AlternateTile) == AnchorType.AlternateTile && tileData2.isValidAlternateAnchor(tileSafely.type))
                            {
                                flag8 = true;
                            }
                        }
                        else if (!flag8 && (anchorBottom.type & AnchorType.EmptyTile) == AnchorType.EmptyTile)
                        {
                            flag8 = true;
                        }
                        if (!flag8)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[num31 + num10, num32 + num11] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[num31 + num10, num32 + num11] = 1;
                        }
                        num24 += 1f;
                    }
                }
                if (tileData2.HookCheckIfCanPlace.hook != null)
                {
                    if (tileData2.HookCheckIfCanPlace.processedCoordinates)
                    {
                        _ = tileData2.Origin;
                        _ = tileData2.Origin;
                    }
                    if (tileData2.HookCheckIfCanPlace.hook(x, y, type, style, dir, num7) == tileData2.HookCheckIfCanPlace.badReturn && tileData2.HookCheckIfCanPlace.badResponse == 0)
                    {
                        num24 = 0f;
                        num22 = 0f;
                        objectPreview.AllInvalid();
                    }
                }
                float num33 = num24 / num25;
                float num34 = num22 / num23;
                if (num34 == 1f && num25 == 0f)
                {
                    num23 = 1f;
                    num25 = 1f;
                    num33 = 1f;
                    num34 = 1f;
                }
                if (num33 == 1f && num34 == 1f)
                {
                    num4           = 1f;
                    num5           = 1f;
                    num6           = num7;
                    tileObjectData = tileData2;
                    break;
                }
                if (num33 > num4 || (num33 == num4 && num34 > num5))
                {
                    TileObjectPreviewData.placementCache.CopyFrom(objectPreview);
                    num4           = num33;
                    num5           = num34;
                    tileObjectData = tileData2;
                    num6           = num7;
                }
            }
            int num35 = -1;

            if (flag)
            {
                if (TileObjectPreviewData.randomCache == null)
                {
                    TileObjectPreviewData.randomCache = new TileObjectPreviewData();
                }
                bool flag9 = false;
                if (TileObjectPreviewData.randomCache.Type == type)
                {
                    Point16 coordinates = TileObjectPreviewData.randomCache.Coordinates;
                    Point16 objectStart = TileObjectPreviewData.randomCache.ObjectStart;
                    int     num36       = coordinates.X + objectStart.X;
                    int     num37       = coordinates.Y + objectStart.Y;
                    int     num38       = x - tileData.Origin.X;
                    int     num39       = y - tileData.Origin.Y;
                    if (num36 != num38 || num37 != num39)
                    {
                        flag9 = true;
                    }
                }
                else
                {
                    flag9 = true;
                }
                num35 = ((!flag9) ? TileObjectPreviewData.randomCache.Random : Main.rand.Next(tileData.RandomStyleRange));
            }
            if (onlyCheck)
            {
                if (num4 != 1f || num5 != 1f)
                {
                    objectPreview.CopyFrom(TileObjectPreviewData.placementCache);
                    num7 = num6;
                }
                objectPreview.Random = num35;
                if (tileData.RandomStyleRange > 0)
                {
                    TileObjectPreviewData.randomCache.CopyFrom(objectPreview);
                }
            }
            if (!onlyCheck)
            {
                objectData.xCoord    = x - tileObjectData.Origin.X;
                objectData.yCoord    = y - tileObjectData.Origin.Y;
                objectData.type      = type;
                objectData.style     = style;
                objectData.alternate = num7;
                objectData.random    = num35;
            }
            if (num4 == 1f)
            {
                return(num5 == 1f);
            }
            return(false);
        }
Пример #2
0
        public static bool CanPlace(int x, int y, int type, int style, int dir, out TileObject objectData, bool onlyCheck = false)
        {
            TileObjectData tileData = TileObjectData.GetTileData(type, style);

            objectData = Empty;
            if (tileData == null)
            {
                return(false);
            }
            int num  = x - tileData.Origin.X;
            int num2 = y - tileData.Origin.Y;

            if (num < 0 || num + tileData.Width >= Main.maxTilesX || num2 < 0 || num2 + tileData.Height >= Main.maxTilesY)
            {
                return(false);
            }
            bool flag = tileData.RandomStyleRange > 0;

            if (TileObjectPreviewData.placementCache == null)
            {
                TileObjectPreviewData.placementCache = new TileObjectPreviewData();
            }
            TileObjectPreviewData.placementCache.Reset();
            int num3 = 0;
            int num4 = 0;

            if (tileData.AlternatesCount != 0)
            {
                num4 = tileData.AlternatesCount;
            }
            float          num5           = -1f;
            float          num6           = -1f;
            int            num7           = 0;
            TileObjectData tileObjectData = null;
            int            num8           = num3 - 1;

            while (num8 < num4)
            {
                num8++;
                TileObjectData tileData2 = TileObjectData.GetTileData(type, style, num8);
                if (tileData2.Direction != 0 && ((tileData2.Direction == TileObjectDirection.PlaceLeft && dir == 1) || (tileData2.Direction == TileObjectDirection.PlaceRight && dir == -1)))
                {
                    continue;
                }
                int num9  = x - tileData2.Origin.X;
                int num10 = y - tileData2.Origin.Y;
                if (num9 < 5 || num9 + tileData2.Width > Main.maxTilesX - 5 || num10 < 5 || num10 + tileData2.Height > Main.maxTilesY - 5)
                {
                    return(false);
                }
                Rectangle rectangle = new Rectangle(0, 0, tileData2.Width, tileData2.Height);
                int       num11     = 0;
                int       num12     = 0;
                if (tileData2.AnchorTop.tileCount != 0)
                {
                    if (rectangle.Y == 0)
                    {
                        rectangle.Y = -1;
                        rectangle.Height++;
                        num12++;
                    }
                    int checkStart = tileData2.AnchorTop.checkStart;
                    if (checkStart < rectangle.X)
                    {
                        rectangle.Width += rectangle.X - checkStart;
                        num11           += rectangle.X - checkStart;
                        rectangle.X      = checkStart;
                    }
                    int num13 = checkStart + tileData2.AnchorTop.tileCount - 1;
                    int num14 = rectangle.X + rectangle.Width - 1;
                    if (num13 > num14)
                    {
                        rectangle.Width += num13 - num14;
                    }
                }
                if (tileData2.AnchorBottom.tileCount != 0)
                {
                    if (rectangle.Y + rectangle.Height == tileData2.Height)
                    {
                        rectangle.Height++;
                    }
                    int checkStart2 = tileData2.AnchorBottom.checkStart;
                    if (checkStart2 < rectangle.X)
                    {
                        rectangle.Width += rectangle.X - checkStart2;
                        num11           += rectangle.X - checkStart2;
                        rectangle.X      = checkStart2;
                    }
                    int num15 = checkStart2 + tileData2.AnchorBottom.tileCount - 1;
                    int num16 = rectangle.X + rectangle.Width - 1;
                    if (num15 > num16)
                    {
                        rectangle.Width += num15 - num16;
                    }
                }
                if (tileData2.AnchorLeft.tileCount != 0)
                {
                    if (rectangle.X == 0)
                    {
                        rectangle.X = -1;
                        rectangle.Width++;
                        num11++;
                    }
                    int num17 = tileData2.AnchorLeft.checkStart;
                    if ((tileData2.AnchorLeft.type & AnchorType.Tree) == AnchorType.Tree)
                    {
                        num17--;
                    }
                    if (num17 < rectangle.Y)
                    {
                        rectangle.Width += rectangle.Y - num17;
                        num12           += rectangle.Y - num17;
                        rectangle.Y      = num17;
                    }
                    int num18 = num17 + tileData2.AnchorLeft.tileCount - 1;
                    if ((tileData2.AnchorLeft.type & AnchorType.Tree) == AnchorType.Tree)
                    {
                        num18 += 2;
                    }
                    int num19 = rectangle.Y + rectangle.Height - 1;
                    if (num18 > num19)
                    {
                        rectangle.Height += num18 - num19;
                    }
                }
                if (tileData2.AnchorRight.tileCount != 0)
                {
                    if (rectangle.X + rectangle.Width == tileData2.Width)
                    {
                        rectangle.Width++;
                    }
                    int num20 = tileData2.AnchorLeft.checkStart;
                    if ((tileData2.AnchorRight.type & AnchorType.Tree) == AnchorType.Tree)
                    {
                        num20--;
                    }
                    if (num20 < rectangle.Y)
                    {
                        rectangle.Width += rectangle.Y - num20;
                        num12           += rectangle.Y - num20;
                        rectangle.Y      = num20;
                    }
                    int num21 = num20 + tileData2.AnchorRight.tileCount - 1;
                    if ((tileData2.AnchorRight.type & AnchorType.Tree) == AnchorType.Tree)
                    {
                        num21 += 2;
                    }
                    int num22 = rectangle.Y + rectangle.Height - 1;
                    if (num21 > num22)
                    {
                        rectangle.Height += num21 - num22;
                    }
                }
                if (onlyCheck)
                {
                    objectPreview.Reset();
                    objectPreview.Active      = true;
                    objectPreview.Type        = (ushort)type;
                    objectPreview.Style       = (short)style;
                    objectPreview.Alternate   = num8;
                    objectPreview.Size        = new Point16(rectangle.Width, rectangle.Height);
                    objectPreview.ObjectStart = new Point16(num11, num12);
                    objectPreview.Coordinates = new Point16(num9 - num11, num10 - num12);
                }
                float num23 = 0f;
                float num24 = tileData2.Width * tileData2.Height;
                float num25 = 0f;
                float num26 = 0f;
                for (int i = 0; i < tileData2.Width; i++)
                {
                    for (int j = 0; j < tileData2.Height; j++)
                    {
                        Tile tileSafely = Framing.GetTileSafely(num9 + i, num10 + j);
                        bool flag2      = !tileData2.LiquidPlace(tileSafely);
                        bool flag3      = false;
                        if (tileData2.AnchorWall)
                        {
                            num26 += 1f;
                            if (!tileData2.isValidWallAnchor(tileSafely.wall))
                            {
                                flag3 = true;
                            }
                            else
                            {
                                num25 += 1f;
                            }
                        }
                        bool flag4 = false;
                        if (tileSafely.active() && !Main.tileCut[tileSafely.type])
                        {
                            flag4 = true;
                        }
                        if (flag4 || flag2 || flag3)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[i + num11, j + num12] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[i + num11, j + num12] = 1;
                        }
                        num23 += 1f;
                    }
                }
                AnchorData anchorBottom = tileData2.AnchorBottom;
                if (anchorBottom.tileCount != 0)
                {
                    num26 += (float)anchorBottom.tileCount;
                    int height = tileData2.Height;
                    for (int k = 0; k < anchorBottom.tileCount; k++)
                    {
                        int  num27      = anchorBottom.checkStart + k;
                        Tile tileSafely = Framing.GetTileSafely(num9 + num27, num10 + height);
                        bool flag5      = false;
                        if (tileSafely.nactive())
                        {
                            if ((anchorBottom.type & AnchorType.SolidTile) == AnchorType.SolidTile && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type] && !Main.tileNoAttach[tileSafely.type] && (tileData2.FlattenAnchors || tileSafely.blockType() == 0))
                            {
                                flag5 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag5 && ((anchorBottom.type & AnchorType.SolidWithTop) == AnchorType.SolidWithTop || (anchorBottom.type & AnchorType.Table) == AnchorType.Table))
                            {
                                if (tileSafely.type == 19)
                                {
                                    int num28 = tileSafely.frameX / TileObjectData.PlatformFrameWidth();
                                    if ((!tileSafely.halfBrick() && num28 >= 0 && num28 <= 7) || (num28 >= 12 && num28 <= 16) || (num28 >= 25 && num28 <= 26))
                                    {
                                        flag5 = true;
                                    }
                                }
                                else if (Main.tileSolid[tileSafely.type] && Main.tileSolidTop[tileSafely.type])
                                {
                                    flag5 = true;
                                }
                            }
                            if (!flag5 && (anchorBottom.type & AnchorType.Table) == AnchorType.Table && tileSafely.type != 19 && Main.tileTable[tileSafely.type] && tileSafely.blockType() == 0)
                            {
                                flag5 = true;
                            }
                            if (!flag5 && (anchorBottom.type & AnchorType.SolidSide) == AnchorType.SolidSide && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type])
                            {
                                switch (tileSafely.blockType())
                                {
                                case 4:
                                case 5:
                                    flag5 = tileData2.isValidTileAnchor(tileSafely.type);
                                    break;
                                }
                            }
                            if (!flag5 && (anchorBottom.type & AnchorType.AlternateTile) == AnchorType.AlternateTile && tileData2.isValidAlternateAnchor(tileSafely.type))
                            {
                                flag5 = true;
                            }
                        }
                        else if (!flag5 && (anchorBottom.type & AnchorType.EmptyTile) == AnchorType.EmptyTile)
                        {
                            flag5 = true;
                        }
                        if (!flag5)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[num27 + num11, height + num12] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[num27 + num11, height + num12] = 1;
                        }
                        num25 += 1f;
                    }
                }
                anchorBottom = tileData2.AnchorTop;
                if (anchorBottom.tileCount != 0)
                {
                    num26 += (float)anchorBottom.tileCount;
                    int num29 = -1;
                    for (int l = 0; l < anchorBottom.tileCount; l++)
                    {
                        int  num30      = anchorBottom.checkStart + l;
                        Tile tileSafely = Framing.GetTileSafely(num9 + num30, num10 + num29);
                        bool flag6      = false;
                        if (tileSafely.nactive())
                        {
                            if (Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type] && !Main.tileNoAttach[tileSafely.type] && (tileData2.FlattenAnchors || tileSafely.blockType() == 0))
                            {
                                flag6 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag6 && (anchorBottom.type & AnchorType.SolidBottom) == AnchorType.SolidBottom && ((Main.tileSolid[tileSafely.type] && (!Main.tileSolidTop[tileSafely.type] || (tileSafely.type == 19 && (tileSafely.halfBrick() || tileSafely.topSlope())))) || tileSafely.halfBrick() || tileSafely.topSlope()) && !TileID.Sets.NotReallySolid[tileSafely.type] && !tileSafely.bottomSlope())
                            {
                                flag6 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag6 && (anchorBottom.type & AnchorType.SolidSide) == AnchorType.SolidSide && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type])
                            {
                                switch (tileSafely.blockType())
                                {
                                case 2:
                                case 3:
                                    flag6 = tileData2.isValidTileAnchor(tileSafely.type);
                                    break;
                                }
                            }
                            if (!flag6 && (anchorBottom.type & AnchorType.AlternateTile) == AnchorType.AlternateTile && tileData2.isValidAlternateAnchor(tileSafely.type))
                            {
                                flag6 = true;
                            }
                        }
                        else if (!flag6 && (anchorBottom.type & AnchorType.EmptyTile) == AnchorType.EmptyTile)
                        {
                            flag6 = true;
                        }
                        if (!flag6)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[num30 + num11, num29 + num12] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[num30 + num11, num29 + num12] = 1;
                        }
                        num25 += 1f;
                    }
                }
                anchorBottom = tileData2.AnchorRight;
                if (anchorBottom.tileCount != 0)
                {
                    num26 += (float)anchorBottom.tileCount;
                    int width = tileData2.Width;
                    for (int m = 0; m < anchorBottom.tileCount; m++)
                    {
                        int  num31      = anchorBottom.checkStart + m;
                        Tile tileSafely = Framing.GetTileSafely(num9 + width, num10 + num31);
                        bool flag7      = false;
                        if (tileSafely.nactive())
                        {
                            if (Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type] && !Main.tileNoAttach[tileSafely.type] && (tileData2.FlattenAnchors || tileSafely.blockType() == 0))
                            {
                                flag7 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag7 && (anchorBottom.type & AnchorType.SolidSide) == AnchorType.SolidSide && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type])
                            {
                                switch (tileSafely.blockType())
                                {
                                case 2:
                                case 4:
                                    flag7 = tileData2.isValidTileAnchor(tileSafely.type);
                                    break;
                                }
                            }
                            if (!flag7 && (anchorBottom.type & AnchorType.Tree) == AnchorType.Tree && tileSafely.type == 5)
                            {
                                flag7 = true;
                                if (m == 0)
                                {
                                    num26 += 1f;
                                    Tile tileSafely2 = Framing.GetTileSafely(num9 + width, num10 + num31 - 1);
                                    if (tileSafely2.nactive() && tileSafely2.type == 5)
                                    {
                                        num25 += 1f;
                                        if (onlyCheck)
                                        {
                                            objectPreview[width + num11, num31 + num12 - 1] = 1;
                                        }
                                    }
                                    else if (onlyCheck)
                                    {
                                        objectPreview[width + num11, num31 + num12 - 1] = 2;
                                    }
                                }
                                if (m == anchorBottom.tileCount - 1)
                                {
                                    num26 += 1f;
                                    Tile tileSafely3 = Framing.GetTileSafely(num9 + width, num10 + num31 + 1);
                                    if (tileSafely3.nactive() && tileSafely3.type == 5)
                                    {
                                        num25 += 1f;
                                        if (onlyCheck)
                                        {
                                            objectPreview[width + num11, num31 + num12 + 1] = 1;
                                        }
                                    }
                                    else if (onlyCheck)
                                    {
                                        objectPreview[width + num11, num31 + num12 + 1] = 2;
                                    }
                                }
                            }
                            if (!flag7 && (anchorBottom.type & AnchorType.AlternateTile) == AnchorType.AlternateTile && tileData2.isValidAlternateAnchor(tileSafely.type))
                            {
                                flag7 = true;
                            }
                        }
                        else if (!flag7 && (anchorBottom.type & AnchorType.EmptyTile) == AnchorType.EmptyTile)
                        {
                            flag7 = true;
                        }
                        if (!flag7)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[width + num11, num31 + num12] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[width + num11, num31 + num12] = 1;
                        }
                        num25 += 1f;
                    }
                }
                anchorBottom = tileData2.AnchorLeft;
                if (anchorBottom.tileCount != 0)
                {
                    num26 += (float)anchorBottom.tileCount;
                    int num32 = -1;
                    for (int n = 0; n < anchorBottom.tileCount; n++)
                    {
                        int  num33      = anchorBottom.checkStart + n;
                        Tile tileSafely = Framing.GetTileSafely(num9 + num32, num10 + num33);
                        bool flag8      = false;
                        if (tileSafely.nactive())
                        {
                            if (Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type] && !Main.tileNoAttach[tileSafely.type] && (tileData2.FlattenAnchors || tileSafely.blockType() == 0))
                            {
                                flag8 = tileData2.isValidTileAnchor(tileSafely.type);
                            }
                            if (!flag8 && (anchorBottom.type & AnchorType.SolidSide) == AnchorType.SolidSide && Main.tileSolid[tileSafely.type] && !Main.tileSolidTop[tileSafely.type])
                            {
                                switch (tileSafely.blockType())
                                {
                                case 3:
                                case 5:
                                    flag8 = tileData2.isValidTileAnchor(tileSafely.type);
                                    break;
                                }
                            }
                            if (!flag8 && (anchorBottom.type & AnchorType.Tree) == AnchorType.Tree && tileSafely.type == 5)
                            {
                                flag8 = true;
                                if (n == 0)
                                {
                                    num26 += 1f;
                                    Tile tileSafely4 = Framing.GetTileSafely(num9 + num32, num10 + num33 - 1);
                                    if (tileSafely4.nactive() && tileSafely4.type == 5)
                                    {
                                        num25 += 1f;
                                        if (onlyCheck)
                                        {
                                            objectPreview[num32 + num11, num33 + num12 - 1] = 1;
                                        }
                                    }
                                    else if (onlyCheck)
                                    {
                                        objectPreview[num32 + num11, num33 + num12 - 1] = 2;
                                    }
                                }
                                if (n == anchorBottom.tileCount - 1)
                                {
                                    num26 += 1f;
                                    Tile tileSafely5 = Framing.GetTileSafely(num9 + num32, num10 + num33 + 1);
                                    if (tileSafely5.nactive() && tileSafely5.type == 5)
                                    {
                                        num25 += 1f;
                                        if (onlyCheck)
                                        {
                                            objectPreview[num32 + num11, num33 + num12 + 1] = 1;
                                        }
                                    }
                                    else if (onlyCheck)
                                    {
                                        objectPreview[num32 + num11, num33 + num12 + 1] = 2;
                                    }
                                }
                            }
                            if (!flag8 && (anchorBottom.type & AnchorType.AlternateTile) == AnchorType.AlternateTile && tileData2.isValidAlternateAnchor(tileSafely.type))
                            {
                                flag8 = true;
                            }
                        }
                        else if (!flag8 && (anchorBottom.type & AnchorType.EmptyTile) == AnchorType.EmptyTile)
                        {
                            flag8 = true;
                        }
                        if (!flag8)
                        {
                            if (onlyCheck)
                            {
                                objectPreview[num32 + num11, num33 + num12] = 2;
                            }
                            continue;
                        }
                        if (onlyCheck)
                        {
                            objectPreview[num32 + num11, num33 + num12] = 1;
                        }
                        num25 += 1f;
                    }
                }
                if (tileData2.HookCheck.hook != null)
                {
                    if (tileData2.HookCheck.processedCoordinates)
                    {
                        short x2 = tileData2.Origin.X;
                        short y2 = tileData2.Origin.Y;
                    }
                    if (tileData2.HookCheck.hook(x, y, type, style, dir) == tileData2.HookCheck.badReturn && tileData2.HookCheck.badResponse == 0)
                    {
                        num25 = 0f;
                        num23 = 0f;
                        objectPreview.AllInvalid();
                    }
                }
                float num34 = num25 / num26;
                float num35 = num23 / num24;
                if (num34 == 1f && num35 == 1f)
                {
                    num5           = 1f;
                    num6           = 1f;
                    num7           = num8;
                    tileObjectData = tileData2;
                    break;
                }
                if (num34 > num5 || (num34 == num5 && num35 > num6))
                {
                    TileObjectPreviewData.placementCache.CopyFrom(objectPreview);
                    num5           = num34;
                    num6           = num35;
                    tileObjectData = tileData2;
                    num7           = num8;
                }
            }
            int num36 = -1;

            if (flag)
            {
                if (TileObjectPreviewData.randomCache == null)
                {
                    TileObjectPreviewData.randomCache = new TileObjectPreviewData();
                }
                bool flag9 = false;
                if (TileObjectPreviewData.randomCache.Type == type)
                {
                    Point16 coordinates = TileObjectPreviewData.randomCache.Coordinates;
                    Point16 objectStart = TileObjectPreviewData.randomCache.ObjectStart;
                    int     num37       = coordinates.X + objectStart.X;
                    int     num38       = coordinates.Y + objectStart.Y;
                    int     num39       = x - tileData.Origin.X;
                    int     num40       = y - tileData.Origin.Y;
                    if (num37 != num39 || num38 != num40)
                    {
                        flag9 = true;
                    }
                }
                else
                {
                    flag9 = true;
                }
                num36 = ((!flag9) ? TileObjectPreviewData.randomCache.Random : Main.rand.Next(tileData.RandomStyleRange));
            }
            if (onlyCheck)
            {
                if (num5 != 1f || num6 != 1f)
                {
                    objectPreview.CopyFrom(TileObjectPreviewData.placementCache);
                    num8 = num7;
                }
                objectPreview.Random = num36;
                if (tileData.RandomStyleRange > 0)
                {
                    TileObjectPreviewData.randomCache.CopyFrom(objectPreview);
                }
            }
            if (!onlyCheck)
            {
                objectData.xCoord    = x - tileObjectData.Origin.X;
                objectData.yCoord    = y - tileObjectData.Origin.Y;
                objectData.type      = type;
                objectData.style     = style;
                objectData.alternate = num8;
                objectData.random    = num36;
            }
            if (num5 == 1f)
            {
                return(num6 == 1f);
            }
            return(false);
        }