// 7 - 1.1.2
        private void AIFriendly(NPC npc, bool flag, Func<Int32, Int32, ITile> TileRefs)
        {
            if (npc.type == NPCType.N142_SANTA_CLAUS && !Main.Xmas)
            {
                npc.StrikeNPC(World.Sender, 9999, 0f, 0);
                NetMessage.SendData(28, -1, -1, "", npc.whoAmI, 9999f, 0f, 0f, 0);
            }
            int num129 = (int)(npc.Position.X + (float)(npc.Width / 2)) / 16;
            int num130 = (int)(npc.Position.Y + (float)npc.Height + 1f) / 16;

            if (num129 < 0 || num129 > Main.maxTilesX)
                return;
            else if (num130 < 0 || num130 > Main.maxTilesY)
                return;

            if (npc.type == NPCType.N107_GOBLIN_TINKERER)
            {
                savedGoblin = true;
            }
            if (npc.type == NPCType.N108_WIZARD)
            {
                savedWizard = true;
            }
            if (npc.type == NPCType.N124_MECHANIC)
            {
                savedMech = true;
            }
            if (npc.type == NPCType.N46_BUNNY && npc.target == 255)
            {
                npc.TargetClosest(true);
            }
            bool flag15 = false;
            npc.directionY = -1;
            if (npc.direction == 0)
            {
                npc.direction = 1;
            }
            for (int num131 = 0; num131 < Main.MAX_PLAYERS; num131++)
            {
                if (Main.players[num131].Active && Main.players[num131].talkNPC == npc.whoAmI)
                {
                    flag15 = true;
                    if (npc.ai[0] != 0f)
                    {
                        npc.netUpdate = true;
                    }
                    npc.ai[0] = 0f;
                    npc.ai[1] = 300f;
                    npc.ai[2] = 100f;
                    if (Main.players[num131].Position.X + (float)(Main.players[num131].Width / 2) < npc.Position.X + (float)(npc.Width / 2))
                    {
                        npc.direction = -1;
                    }
                    else
                    {
                        npc.direction = 1;
                    }
                }
            }
            if (npc.ai[3] > 0f)
            {
                npc.life = -1;
                npc.HitEffect(0, 10.0);
                npc.Active = false;
            }
            if (npc.type == NPCType.N37_OLD_MAN)
            {
                npc.homeless = false;
                npc.homeTileX = Main.dungeonX;
                npc.homeTileY = Main.dungeonY;
                if (downedBoss3)
                {
                    npc.ai[3] = 1f;
                    npc.netUpdate = true;
                }
            }
            int num132 = npc.homeTileY;
            if (npc.homeTileY > 0)
            {
                while (!WorldModify.SolidTile(TileRefs, npc.homeTileX, num132) && num132 < Main.maxTilesY - 20)
                {
                    num132++;
                }
            }
            var type = (int)Main.tile.At(num129, num130).Type;
            if (npc.townNPC && (!Main.dayTime || Main.tileDungeon[type]) && (num129 != npc.homeTileX || num130 != num132) && !npc.homeless)
            {
                bool flag16 = true;
                for (int num133 = 0; num133 < 2; num133++)
                {
                    Rectangle rectangle3 = new Rectangle((int)(npc.Position.X + (float)(npc.Width / 2) - (float)(sWidth / 2) - (float)safeRangeX), (int)(npc.Position.Y + (float)(npc.Height / 2) - (float)(sHeight / 2) - (float)safeRangeY), sWidth + safeRangeX * 2, sHeight + safeRangeY * 2);
                    if (num133 == 1)
                    {
                        rectangle3 = new Rectangle(npc.homeTileX * 16 + 8 - sWidth / 2 - safeRangeX, num132 * 16 + 8 - sHeight / 2 - safeRangeY, sWidth + safeRangeX * 2, sHeight + safeRangeY * 2);
                    }
                    for (int num134 = 0; num134 < 255; num134++)
                    {
                        if (Main.players[num134].Active)
                        {
                            Rectangle rectangle4 = new Rectangle((int)Main.players[num134].Position.X, (int)Main.players[num134].Position.Y,
                                Main.players[num134].Width, Main.players[num134].Height);
                            if (rectangle4.Intersects(rectangle3))
                            {
                                flag16 = false;
                                break;
                            }
                        }
                        if (!flag16)
                        {
                            break;
                        }
                    }
                }
                if (flag16)
                {
                    if (npc.type == NPCType.N37_OLD_MAN || !Collision.SolidTiles(npc.homeTileX - 1, npc.homeTileX + 1, num132 - 3, num132 - 1))
                    {
                        npc.Velocity.X = 0f;
                        npc.Velocity.Y = 0f;
                        npc.Position.X = (float)(npc.homeTileX * 16 + 8 - npc.Width / 2);
                        npc.Position.Y = (float)(num132 * 16 - npc.Height) - 0.1f;
                        npc.netUpdate = true;
                    }
                    else
                    {
                        npc.homeless = true;
                        WorldModify.QuickFindHome(TileRefs, npc.whoAmI);
                    }
                }
            }
            if (npc.ai[0] == 0f)
            {
                if (npc.ai[2] > 0f)
                {
                    npc.ai[2] -= 1f;
                }
                if (!Main.dayTime && !flag15 && npc.type != NPCType.N46_BUNNY)
                {
                    if (num129 == npc.homeTileX && num130 == num132)
                    {
                        if (npc.Velocity.X != 0f)
                        {
                            npc.netUpdate = true;
                        }
                        if ((double)npc.Velocity.X > 0.1)
                        {
                            npc.Velocity.X = npc.Velocity.X - 0.1f;
                        }
                        else if ((double)npc.Velocity.X < -0.1)
                        {
                            npc.Velocity.X = npc.Velocity.X + 0.1f;
                        }
                        else
                        {
                            npc.Velocity.X = 0f;
                        }
                    }
                    else if (!flag15)
                    {
                        if (num129 > npc.homeTileX)
                        {
                            npc.direction = -1;
                        }
                        else
                        {
                            npc.direction = 1;
                        }
                        npc.ai[0] = 1f;
                        npc.ai[1] = (float)(200 + Main.rand.Next(200));
                        npc.ai[2] = 0f;
                        npc.netUpdate = true;
                    }
                }
                else
                {
                    if ((double)npc.Velocity.X > 0.1)
                    {
                        npc.Velocity.X = npc.Velocity.X - 0.1f;
                    }
                    else if ((double)npc.Velocity.X < -0.1)
                    {
                        npc.Velocity.X = npc.Velocity.X + 0.1f;
                    }
                    else
                    {
                        npc.Velocity.X = 0f;
                    }

                    if (npc.ai[1] > 0f)
                    {
                        npc.ai[1] -= 1f;
                    }
                    if (npc.ai[1] <= 0f)
                    {
                        npc.ai[0] = 1f;
                        npc.ai[1] = (float)(200 + Main.rand.Next(200));
                        if (npc.type == NPCType.N46_BUNNY)
                        {
                            npc.ai[1] += (float)Main.rand.Next(200, 400);
                        }
                        npc.ai[2] = 0f;
                        npc.netUpdate = true;
                    }
                }
                if ((Main.dayTime || (num129 == npc.homeTileX && num130 == num132)))
                {
                    if (num129 < npc.homeTileX - 25 || num129 > npc.homeTileX + 25)
                    {
                        if (npc.ai[2] == 0f)
                        {
                            if (num129 < npc.homeTileX - 50 && npc.direction == -1)
                            {
                                npc.direction = 1;
                                npc.netUpdate = true;
                                return;
                            }
                            if (num129 > npc.homeTileX + 50 && npc.direction == 1)
                            {
                                npc.direction = -1;
                                npc.netUpdate = true;
                                return;
                            }
                        }
                    }
                    else if (Main.rand.Next(80) == 0 && npc.ai[2] == 0f)
                    {
                        npc.ai[2] = 200f;
                        npc.direction *= -1;
                        npc.netUpdate = true;
                        return;
                    }
                }
            }
            else
            {
                if (npc.ai[0] == 1f)
                {
                    if (!Main.dayTime && num129 == npc.homeTileX && num130 == npc.homeTileY && npc.type != NPCType.N46_BUNNY)
                    {
                        npc.ai[0] = 0f;
                        npc.ai[1] = (float)(200 + Main.rand.Next(200));
                        npc.ai[2] = 60f;
                        npc.netUpdate = true;
                        return;
                    }
                    if (!npc.homeless && !Main.tileDungeon[(int)Main.tile.At(num129, num130).Type] && (num129 < npc.homeTileX - 35 || num129 > npc.homeTileX + 35))
                    {
                        if (npc.Position.X < (float)(npc.homeTileX * 16) && npc.direction == -1)
                        {
                            npc.ai[1] -= 5f;
                        }
                        else if (npc.Position.X > (float)(npc.homeTileX * 16) && npc.direction == 1)
                        {
                            npc.ai[1] -= 5f;
                        }
                    }
                    npc.ai[1] -= 1f;
                    if (npc.ai[1] <= 0f)
                    {
                        npc.ai[0] = 0f;
                        npc.ai[1] = (float)(300 + Main.rand.Next(300));
                        if (npc.type == NPCType.N46_BUNNY)
                        {
                            npc.ai[1] -= (float)Main.rand.Next(100);
                        }
                        npc.ai[2] = 60f;
                        npc.netUpdate = true;
                    }
                    if (npc.closeDoor && ((npc.Position.X + (float)(npc.Width / 2)) / 16f > (float)(npc.doorX + 2) || (npc.Position.X + (float)(npc.Width / 2)) / 16f < (float)(npc.doorX - 2)))
                    {
                        bool flag17 = WorldModify.CloseDoor(TileRefs, null, npc.doorX, npc.doorY, false, npc);
                        if (flag17)
                        {
                            npc.closeDoor = false;
                            NetMessage.SendData(19, -1, -1, "", 1, (float)npc.doorX, (float)npc.doorY, (float)npc.direction, 0);
                        }
                        if ((npc.Position.X + (float)(npc.Width / 2)) / 16f > (float)(npc.doorX + 4) || (npc.Position.X + (float)(npc.Width / 2)) / 16f < (float)(npc.doorX - 4) || (npc.Position.Y + (float)(npc.Height / 2)) / 16f > (float)(npc.doorY + 4) || (npc.Position.Y + (float)(npc.Height / 2)) / 16f < (float)(npc.doorY - 4))
                        {
                            npc.closeDoor = false;
                        }
                    }
                    if (npc.Velocity.X < -1f || npc.Velocity.X > 1f)
                    {
                        if (npc.Velocity.Y == 0f)
                        {
                            npc.Velocity *= 0.8f;
                        }
                    }
                    else if ((double)npc.Velocity.X < 1.15 && npc.direction == 1)
                    {
                        npc.Velocity.X = npc.Velocity.X + 0.07f;
                        if (npc.Velocity.X > 1f)
                        {
                            npc.Velocity.X = 1f;
                        }
                    }
                    else if (npc.Velocity.X > -1f && npc.direction == -1)
                    {
                        npc.Velocity.X = npc.Velocity.X - 0.07f;
                        if (npc.Velocity.X > 1f)
                        {
                            npc.Velocity.X = 1f;
                        }
                    }

                    if (npc.Velocity.Y == 0f)
                    {
                        if (npc.Position.X == npc.ai[2])
                        {
                            npc.direction *= -1;
                        }
                        npc.ai[2] = -1f;
                        int num135 = (int)((npc.Position.X + (float)(npc.Width / 2) + (float)(15 * npc.direction)) / 16f);
                        int num136 = (int)((npc.Position.Y + (float)npc.Height - 16f) / 16f);

                        if (npc.townNPC && Main.tile.At(num135, num136 - 2).Active && Main.tile.At(num135, num136 - 2).Type == 10 &&
                            (Main.rand.Next(10) == 0 || !Main.dayTime))
                        {
                            bool flag18 = WorldModify.OpenDoor(TileRefs, null, num135, num136 - 2, npc.direction, npc);
                            if (flag18)
                            {
                                npc.closeDoor = true;
                                npc.doorX = num135;
                                npc.doorY = num136 - 2;
                                NetMessage.SendData(19, -1, -1, "", 0, (float)num135, (float)(num136 - 2), (float)npc.direction, 0);
                                npc.netUpdate = true;
                                npc.ai[1] += 80f;
                                return;
                            }
                            if (WorldModify.OpenDoor(TileRefs, null, num135, num136 - 2, -npc.direction, npc))
                            {
                                npc.closeDoor = true;
                                npc.doorX = num135;
                                npc.doorY = num136 - 2;
                                NetMessage.SendData(19, -1, -1, "", 0, (float)num135, (float)(num136 - 2), (float)(-(float)npc.direction), 0);
                                npc.netUpdate = true;
                                npc.ai[1] += 80f;
                                return;
                            }
                            npc.direction *= -1;
                            npc.netUpdate = true;
                            return;
                        }
                        else
                        {
                            if ((npc.Velocity.X < 0f && npc.spriteDirection == -1) || (npc.Velocity.X > 0f && npc.spriteDirection == 1))
                            {
                                if (Main.tile.At(num135, num136 - 2).Active && Main.tileSolid[(int)Main.tile.At(num135, num136 - 2).Type] &&
                                    !Main.tileSolidTop[(int)Main.tile.At(num135, num136 - 2).Type])
                                {
                                    if ((npc.direction == 1 && !Collision.SolidTiles(num135 - 2, num135 - 1, num136 - 5, num136 - 1)) ||
                                        (npc.direction == -1 && !Collision.SolidTiles(num135 + 1, num135 + 2, num136 - 5, num136 - 1)))
                                    {
                                        if (!Collision.SolidTiles(num135, num135, num136 - 5, num136 - 3))
                                        {
                                            npc.Velocity.Y = -6f;
                                            npc.netUpdate = true;
                                        }
                                        else
                                        {
                                            npc.direction *= -1;
                                            npc.netUpdate = true;
                                        }
                                    }
                                    else
                                    {
                                        npc.direction *= -1;
                                        npc.netUpdate = true;
                                    }
                                }
                                else
                                {
                                    if (Main.tile.At(num135, num136 - 1).Active && Main.tileSolid[(int)Main.tile.At(num135, num136 - 1).Type] &&
                                        !Main.tileSolidTop[(int)Main.tile.At(num135, num136 - 1).Type])
                                    {
                                        if ((npc.direction == 1 && !Collision.SolidTiles(num135 - 2, num135 - 1, num136 - 4, num136 - 1)) ||
                                            (npc.direction == -1 && !Collision.SolidTiles(num135 + 1, num135 + 2, num136 - 4, num136 - 1)))
                                        {
                                            if (!Collision.SolidTiles(num135, num135, num136 - 4, num136 - 2))
                                            {
                                                npc.Velocity.Y = -5f;
                                                npc.netUpdate = true;
                                            }
                                            else
                                            {
                                                npc.direction *= -1;
                                                npc.netUpdate = true;
                                            }
                                        }
                                        else
                                        {
                                            npc.direction *= -1;
                                            npc.netUpdate = true;
                                        }
                                    }
                                    else if (Main.tile.At(num135, num136).Active && Main.tileSolid[(int)Main.tile.At(num135, num136).Type] &&
                                            !Main.tileSolidTop[(int)Main.tile.At(num135, num136).Type])
                                    {
                                        if ((npc.direction == 1 && !Collision.SolidTiles(num135 - 2, num135, num136 - 3, num136 - 1)) ||
                                            (npc.direction == -1 && !Collision.SolidTiles(num135, num135 + 2, num136 - 3, num136 - 1)))
                                        {
                                            npc.Velocity.Y = -3.6f;
                                            npc.netUpdate = true;
                                        }
                                        else
                                        {
                                            npc.direction *= -1;
                                            npc.netUpdate = true;
                                        }
                                    }
                                }
                                try
                                {
                                    if (num129 >= npc.homeTileX - 35 && num129 <= npc.homeTileX + 35 && (!Main.tile.At(num135, num136 + 1).Active ||
                                        !Main.tileSolid[(int)Main.tile.At(num135, num136 + 1).Type]) && (!Main.tile.At(num135 - npc.direction, num136 + 1).Active ||
                                            !Main.tileSolid[(int)Main.tile.At(num135 - npc.direction, num136 + 1).Type]) && (!Main.tile.At(num135, num136 + 2).Active ||
                                                !Main.tileSolid[(int)Main.tile.At(num135, num136 + 2).Type]) && (!Main.tile.At(num135 - npc.direction, num136 + 2).Active ||
                                                    !Main.tileSolid[(int)Main.tile.At(num135 - npc.direction, num136 + 2).Type]) &&
                                                        (!Main.tile.At(num135, num136 + 3).Active || !Main.tileSolid[(int)Main.tile.At(num135, num136 + 3).Type]) &&
                                                            (!Main.tile.At(num135 - npc.direction, num136 + 3).Active ||
                                                                !Main.tileSolid[(int)Main.tile.At(num135 - npc.direction, num136 + 3).Type]) &&
                                                                    (!Main.tile.At(num135, num136 + 4).Active || !Main.tileSolid[(int)Main.tile.At(num135, num136 + 4).Type]) &&
                                                                        (!Main.tile.At(num135 - npc.direction, num136 + 4).Active ||
                                                                            !Main.tileSolid[(int)Main.tile.At(num135 - npc.direction, num136 + 4).Type])
                                                                                && npc.type != NPCType.N46_BUNNY)
                                    {
                                        npc.direction *= -1;
                                        npc.Velocity.X = npc.Velocity.X * -1f;
                                        npc.netUpdate = true;
                                    }
                                }
                                catch
                                { }

                                if (npc.Velocity.Y < 0f)
                                {
                                    npc.ai[2] = npc.Position.X;
                                }
                            }
                            if (npc.Velocity.Y < 0f && npc.wet)
                            {
                                npc.Velocity.Y = npc.Velocity.Y * 1.2f;
                            }
                            if (npc.Velocity.Y < 0f && npc.type == NPCType.N46_BUNNY)
                            {
                                npc.Velocity.Y = npc.Velocity.Y * 1.2f;
                                return;
                            }
                        }
                    }
                }
            }
        }
        // 9 - 1.1.2
        private void AISphere(NPC npc, bool flag, Func<Int32, Int32, ITile> TileRefs)
        {
            if (npc.target == 255)
            {
                npc.TargetClosest(true);
                float num157 = 6f;
                if (npc.type == NPCType.N25_BURNING_SPHERE)
                {
                    num157 = 5f;
                }
                if (npc.type == NPCType.N112_VILE_SPIT)
                {
                    num157 = 7f;
                }
                Vector2 vector15 = new Vector2(npc.Position.X + (float)npc.Width * 0.5f, npc.Position.Y + (float)npc.Height * 0.5f);
                float num158 = Main.players[npc.target].Position.X + (float)(Main.players[npc.target].Width / 2) - vector15.X;
                float num159 = Main.players[npc.target].Position.Y + (float)(Main.players[npc.target].Height / 2) - vector15.Y;
                float num160 = (float)Math.Sqrt((double)(num158 * num158 + num159 * num159));
                num160 = num157 / num160;
                npc.Velocity.X = num158 * num160;
                npc.Velocity.Y = num159 * num160;
            }
            if (npc.type == NPCType.N112_VILE_SPIT)
            {
                npc.ai[0] += 1f;
                if (npc.ai[0] > 3f)
                {
                    npc.ai[0] = 3f;
                }
                if (npc.ai[0] == 2f)
                {
                    npc.Position += npc.Velocity;
                }
            }
            if (npc.type == NPCType.N112_VILE_SPIT && Collision.SolidCollision(npc.Position, npc.Width, npc.Height))
            {
                int num163 = (int)(npc.Position.X + (float)(npc.Width / 2)) / 16;
                int num164 = (int)(npc.Position.Y + (float)(npc.Height / 2)) / 16;
                int num165 = 8;
                for (int num166 = num163 - num165; num166 <= num163 + num165; num166++)
                {
                    for (int num167 = num164 - num165; num167 < num164 + num165; num167++)
                    {
                        if ((double)(Math.Abs(num166 - num163) + Math.Abs(num167 - num164)) < (double)num165 * 0.5)
                        {
                            if (Main.tile.At(num166, num167).Type == 2)
                            {
                                Main.tile.At(num166, num167).SetType(23);
                                WorldModify.SquareTileFrame(TileRefs, null, num166, num167, true);
                                NetMessage.SendTileSquare(-1, num166, num167, 1);
                            }
                            else if (Main.tile.At(num166, num167).Type == 1)
                            {
                                Main.tile.At(num166, num167).SetType(25);
                                WorldModify.SquareTileFrame(TileRefs, null, num166, num167, true);
                                NetMessage.SendTileSquare(-1, num166, num167, 1);
                            }
                            else if (Main.tile.At(num166, num167).Type == 53)
                            {
                                Main.tile.At(num166, num167).SetType(112);
                                WorldModify.SquareTileFrame(TileRefs, null, num166, num167, true);
                                NetMessage.SendTileSquare(-1, num166, num167, 1);
                            }
                            else if (Main.tile.At(num166, num167).Type == 109)
                            {
                                Main.tile.At(num166, num167).SetType(23);
                                WorldModify.SquareTileFrame(TileRefs, null, num166, num167, true);
                                NetMessage.SendTileSquare(-1, num166, num167, 1);
                            }
                            else if (Main.tile.At(num166, num167).Type == 117)
                            {
                                Main.tile.At(num166, num167).SetType(25);
                                WorldModify.SquareTileFrame(TileRefs, null, num166, num167, true);
                                NetMessage.SendTileSquare(-1, num166, num167, 1);
                            }
                            else if (Main.tile.At(num166, num167).Type == 116)
                            {
                                Main.tile.At(num166, num167).SetType(112);
                                WorldModify.SquareTileFrame(TileRefs, null, num166, num167, true);
                                NetMessage.SendTileSquare(-1, num166, num167, 1);
                            }
                        }
                    }
                }
                npc.StrikeNPC(World.Sender, 999, 0f, 0, false);
            }
            if (npc.timeLeft > 100)
            {
                npc.timeLeft = 100;
            }

            npc.rotation += 0.4f * (float)npc.direction;
            return;
        }