// 6
 private void AIWorm(NPC npc, bool flag)
 {
     if (npc.target < 0 || npc.target == 255 || Main.players[npc.target].dead)
     {
         npc.TargetClosest(true);
     }
     if (Main.players[npc.target].dead && npc.timeLeft > 10)
     {
         npc.timeLeft = 10;
     }
     if ((npc.Type == 7 || npc.Type == 8 || npc.Type == 10 || npc.Type == 11 || npc.Type == 13 || npc.Type == 14 || npc.Type == 39 || npc.Type == 40) && npc.ai[0] == 0f)
     {
         if (npc.Type == 7 || npc.Type == 10 || npc.Type == 13 || npc.Type == 39)
         {
             npc.ai[2] = (float)Main.rand.Next(8, 13);
             if (npc.Type == 10)
             {
                 npc.ai[2] = (float)Main.rand.Next(4, 7);
             }
             if (npc.Type == 13)
             {
                 npc.ai[2] = (float)Main.rand.Next(45, 56);
             }
             if (npc.Type == 39)
             {
                 npc.ai[2] = (float)Main.rand.Next(12, 19);
             }
             npc.ai[0] = (float)NPC.NewNPC((int)(npc.Position.X + (float)(npc.Width / 2)), (int)(npc.Position.Y + (float)npc.Height), npc.Type + 1, npc.whoAmI);
         }
         else
         {
             if ((npc.Type == 8 || npc.Type == 11 || npc.Type == 14 || npc.Type == 40) && npc.ai[2] > 0f)
             {
                 npc.ai[0] = (float)NPC.NewNPC((int)(npc.Position.X + (float)(npc.Width / 2)), (int)(npc.Position.Y + (float)npc.Height), npc.Type, npc.whoAmI);
             }
             else
             {
                 npc.ai[0] = (float)NPC.NewNPC((int)(npc.Position.X + (float)(npc.Width / 2)), (int)(npc.Position.Y + (float)npc.Height), npc.Type + 1, npc.whoAmI);
             }
         }
         Main.npcs[(int)npc.ai[0]].ai[1] = (float)npc.whoAmI;
         Main.npcs[(int)npc.ai[0]].ai[2] = npc.ai[2] - 1f;
         npc.netUpdate = true;
     }
     if ((npc.Type == 8 || npc.Type == 9 || npc.Type == 11 || npc.Type == 12 || npc.Type == 40 || npc.Type == 41) && (!Main.npcs[(int)npc.ai[1]].Active || Main.npcs[(int)npc.ai[1]].aiStyle != npc.aiStyle))
     {
         npc.life = 0;
         npc.HitEffect(0, 10.0);
         npc.Active = false;
     }
     if ((npc.Type == 7 || npc.Type == 8 || npc.Type == 10 || npc.Type == 11 || npc.Type == 39 || npc.Type == 40) && !Main.npcs[(int)npc.ai[0]].Active)
     {
         npc.life = 0;
         npc.HitEffect(0, 10.0);
         npc.Active = false;
     }
     if (npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD || npc.type == NPCType.N14_EATER_OF_WORLDS_BODY || npc.type == NPCType.N15_EATER_OF_WORLDS_TAIL)
     {
         //If this segment has no segment before or after it, DIE!
         if (!Main.npcs[(int)npc.ai[1]].Active && !Main.npcs[(int)npc.ai[0]].Active)
         {
             npc.life = 0;
             npc.HitEffect(0, 10.0);
             npc.Active = false;
         }
         //If we are a head connected to nothing, DIE!
         if (npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD && !Main.npcs[(int)npc.ai[0]].Active)
         {
             npc.life = 0;
             npc.HitEffect(0, 10.0);
             npc.Active = false;
         }
         //If we are a tail connected to nothing, DIE!
         if (npc.type == NPCType.N15_EATER_OF_WORLDS_TAIL && !Main.npcs[(int)npc.ai[1]].Active)
         {
             npc.life = 0;
             npc.HitEffect(0, 10.0);
             npc.Active = false;
         }
         //If the next segment forward has died, become a head
         if (npc.type == NPCType.N14_EATER_OF_WORLDS_BODY && !Main.npcs[(int)npc.ai[1]].Active)
         {
             npc.type = NPCType.N13_EATER_OF_WORLDS_HEAD;
             int num52 = npc.whoAmI;
             float num53 = (float)npc.life / (float)npc.lifeMax;
             float num54 = npc.ai[0];
             //npc.SetDefaults(npc.Type, -1f);
             //npc = Registries.NPC.Create(npc.Type); WTF!?
             Registries.NPC.SetDefaults(npc, 13); //FIXME: remember to tweak
             npc.Active = true;
             npc.life = (int)((float)npc.lifeMax * num53);
             npc.ai[0] = num54;
             npc.TargetClosest(true);
             npc.netUpdate = true;
             npc.whoAmI = num52;
         }
         //If the next segment behind us has died, become a tail
         if (npc.type == NPCType.N14_EATER_OF_WORLDS_BODY && !Main.npcs[(int)npc.ai[0]].Active)
         {
             npc.type = NPCType.N15_EATER_OF_WORLDS_TAIL;
             int num55 = npc.whoAmI;
             float num56 = (float)npc.life / (float)npc.lifeMax;
             float num57 = npc.ai[1];
             //npc.SetDefaults(npc.Type, -1f);
             //npc = Registries.NPC.Create(npc.Type);
             Registries.NPC.SetDefaults(npc, 14); //FIXME: remember to tweak
             npc.Active = true;
             npc.life = (int)((float)npc.lifeMax * num56);
             npc.ai[1] = num57;
             npc.TargetClosest(true);
             npc.netUpdate = true;
             npc.whoAmI = num55;
         }
         if (npc.life == 0)
         {
             bool flag6 = true;
             for (int l = 0; l < MAX_NPCS; l++)
             {
                 if (Main.npcs[l].Active && (Main.npcs[l].type == NPCType.N13_EATER_OF_WORLDS_HEAD ||
                                              Main.npcs[l].type == NPCType.N14_EATER_OF_WORLDS_BODY ||
                                              Main.npcs[l].type == NPCType.N15_EATER_OF_WORLDS_TAIL))
                 {
                     flag6 = false;
                     break;
                 }
             }
             if (flag6)
             {
                 npc.boss = true;
                 npc.NPCLoot();
             }
         }
     }
     if (!npc.Active)
     {
         NetMessage.SendData(28, -1, -1, "", npc.whoAmI, -1f, 0f, 0f, 0);
     }
     int num58 = (int)(npc.Position.X / 16f) - 1;
     int num59 = (int)((npc.Position.X + (float)npc.Width) / 16f) + 2;
     int num60 = (int)(npc.Position.Y / 16f) - 1;
     int num61 = (int)((npc.Position.Y + (float)npc.Height) / 16f) + 2;
     if (num58 < 0)
     {
         num58 = 0;
     }
     if (num59 > Main.maxTilesX)
     {
         num59 = Main.maxTilesX;
     }
     if (num60 < 0)
     {
         num60 = 0;
     }
     if (num61 > Main.maxTilesY)
     {
         num61 = Main.maxTilesY;
     }
     bool flag7 = false;
     for (int m = num58; m < num59; m++)
     {
         for (int n = num60; n < num61; n++)
         {
             if (Main.tile.At(m, n).Exists && ((Main.tile.At(m, n).Active && (Main.tileSolid[(int)Main.tile.At(m, n).Type] || (Main.tileSolidTop[(int)Main.tile.At(m, n).Type] && Main.tile.At(m, n).FrameY == 0))) || Main.tile.At(m, n).Liquid > 64))
             {
                 Vector2 vector9;
                 vector9.X = (float)(m * 16);
                 vector9.Y = (float)(n * 16);
                 if (npc.Position.X + (float)npc.Width > vector9.X && npc.Position.X < vector9.X + 16f && npc.Position.Y + (float)npc.Height > vector9.Y && npc.Position.Y < vector9.Y + 16f)
                 {
                     flag7 = true;
                     if (Main.rand.Next(40) == 0 && Main.tile.At(m, n).Active)
                     {
                         WorldModify.KillTile(m, n, true, true, false);
                     }
                     //if (Main.tile.At(m, n).Type == 2)
                     //{
                     //    byte arg_4656_0 = Main.tile.At(m, n - 1).Type;
                     //}
                 }
             }
         }
     }
     if (!flag7 && (npc.Type == 7 || npc.Type == 10 || npc.Type == 13 || npc.Type == 39))
     {
         Rectangle rectangle = new Rectangle((int)npc.Position.X, (int)npc.Position.Y, npc.Width, npc.Height);
         int num62 = 1000;
         bool flag8 = true;
         for (int num63 = 0; num63 < 255; num63++)
         {
             if (Main.players[num63].Active)
             {
                 Rectangle rectangle2 = new Rectangle((int)Main.players[num63].Position.X - num62, (int)Main.players[num63].Position.Y - num62, num62 * 2, num62 * 2);
                 if (rectangle.Intersects(rectangle2))
                 {
                     flag8 = false;
                     break;
                 }
             }
         }
         if (flag8)
         {
             flag7 = true;
         }
     }
     float num64 = 8f;
     float num65 = 0.07f;
     if (npc.Type == 10)
     {
         num64 = 6f;
         num65 = 0.05f;
     }
     if (npc.Type == 13)
     {
         num64 = 10f;
         num65 = 0.07f;
     }
     Vector2 vector10 = new Vector2(npc.Position.X + (float)npc.Width * 0.5f, npc.Position.Y + (float)npc.Height * 0.5f);
     float num66 = Main.players[npc.target].Position.X + (float)(Main.players[npc.target].Width / 2) - vector10.X;
     float num67 = Main.players[npc.target].Position.Y + (float)(Main.players[npc.target].Height / 2) - vector10.Y;
     float num68 = (float)Math.Sqrt((double)(num66 * num66 + num67 * num67));
     if (npc.ai[1] > 0f)
     {
         num66 = Main.npcs[(int)npc.ai[1]].Position.X + (float)(Main.npcs[(int)npc.ai[1]].Width / 2) - vector10.X;
         num67 = Main.npcs[(int)npc.ai[1]].Position.Y + (float)(Main.npcs[(int)npc.ai[1]].Height / 2) - vector10.Y;
         npc.rotation = (float)Math.Atan2((double)num67, (double)num66) + 1.57f;
         num68 = (float)Math.Sqrt((double)(num66 * num66 + num67 * num67));
         num68 = (num68 - (float)npc.Width) / num68;
         num66 *= num68;
         num67 *= num68;
         npc.Velocity = default(Vector2);
         npc.Position.X = npc.Position.X + num66;
         npc.Position.Y = npc.Position.Y + num67;
         return;
     }
     if (!flag7)
     {
         npc.TargetClosest(true);
         npc.Velocity.Y = npc.Velocity.Y + 0.11f;
         if (npc.Velocity.Y > num64)
         {
             npc.Velocity.Y = num64;
         }
         if ((double)(Math.Abs(npc.Velocity.X) + Math.Abs(npc.Velocity.Y)) < (double)num64 * 0.4)
         {
             if (npc.Velocity.X < 0f)
             {
                 npc.Velocity.X = npc.Velocity.X - num65 * 1.1f;
             }
             else
             {
                 npc.Velocity.X = npc.Velocity.X + num65 * 1.1f;
             }
         }
         else
         {
             if (npc.Velocity.Y == num64)
             {
                 if (npc.Velocity.X < num66)
                 {
                     npc.Velocity.X = npc.Velocity.X + num65;
                 }
                 else
                 {
                     if (npc.Velocity.X > num66)
                     {
                         npc.Velocity.X = npc.Velocity.X - num65;
                     }
                 }
             }
             else
             {
                 if (npc.Velocity.Y > 4f)
                 {
                     if (npc.Velocity.X < 0f)
                     {
                         npc.Velocity.X = npc.Velocity.X + num65 * 0.9f;
                     }
                     else
                     {
                         npc.Velocity.X = npc.Velocity.X - num65 * 0.9f;
                     }
                 }
             }
         }
     }
     else
     {
         if (npc.soundDelay == 0)
         {
             float num69 = num68 / 40f;
             if (num69 < 10f)
             {
                 num69 = 10f;
             }
             if (num69 > 20f)
             {
                 num69 = 20f;
             }
             npc.soundDelay = (int)num69;
         }
         num68 = (float)Math.Sqrt((double)(num66 * num66 + num67 * num67));
         float num70 = Math.Abs(num66);
         float num71 = Math.Abs(num67);
         num68 = num64 / num68;
         num66 *= num68;
         num67 *= num68;
         if ((npc.Type == 13 || npc.Type == 7) && !Main.players[npc.target].zoneEvil)
         {
             bool flag9 = true;
             for (int num72 = 0; num72 < 255; num72++)
             {
                 if (Main.players[num72].Active && !Main.players[num72].dead && Main.players[num72].zoneEvil)
                 {
                     flag9 = false;
                 }
             }
             if (flag9)
             {
                 if ((double)(npc.Position.Y / 16f) > (Main.rockLayer + (double)Main.maxTilesY) / 2.0)
                 {
                     npc.Active = false;
                     int num73 = (int)npc.ai[0];
                     while (num73 > 0 && num73 < MAX_NPCS && Main.npcs[num73].Active && Main.npcs[num73].aiStyle == npc.aiStyle)
                     {
                         int num74 = (int)Main.npcs[num73].ai[0];
                         Main.npcs[num73].Active = false;
                         npc.life = 0;
                         NetMessage.SendData(23, -1, -1, "", num73, 0f, 0f, 0f, 0);
                         num73 = num74;
                     }
                     NetMessage.SendData(23, -1, -1, "", npc.whoAmI, 0f, 0f, 0f, 0);
                 }
                 num66 = 0f;
                 num67 = num64;
             }
         }
         if ((npc.Velocity.X > 0f && num66 > 0f) || (npc.Velocity.X < 0f && num66 < 0f) || (npc.Velocity.Y > 0f && num67 > 0f) || (npc.Velocity.Y < 0f && num67 < 0f))
         {
             if (npc.Velocity.X < num66)
             {
                 npc.Velocity.X = npc.Velocity.X + num65;
             }
             else
             {
                 if (npc.Velocity.X > num66)
                 {
                     npc.Velocity.X = npc.Velocity.X - num65;
                 }
             }
             if (npc.Velocity.Y < num67)
             {
                 npc.Velocity.Y = npc.Velocity.Y + num65;
             }
             else
             {
                 if (npc.Velocity.Y > num67)
                 {
                     npc.Velocity.Y = npc.Velocity.Y - num65;
                 }
             }
         }
         else
         {
             if (num70 > num71)
             {
                 if (npc.Velocity.X < num66)
                 {
                     npc.Velocity.X = npc.Velocity.X + num65 * 1.1f;
                 }
                 else
                 {
                     if (npc.Velocity.X > num66)
                     {
                         npc.Velocity.X = npc.Velocity.X - num65 * 1.1f;
                     }
                 }
                 if ((double)(Math.Abs(npc.Velocity.X) + Math.Abs(npc.Velocity.Y)) < (double)num64 * 0.5)
                 {
                     if (npc.Velocity.Y > 0f)
                     {
                         npc.Velocity.Y = npc.Velocity.Y + num65;
                     }
                     else
                     {
                         npc.Velocity.Y = npc.Velocity.Y - num65;
                     }
                 }
             }
             else
             {
                 if (npc.Velocity.Y < num67)
                 {
                     npc.Velocity.Y = npc.Velocity.Y + num65 * 1.1f;
                 }
                 else
                 {
                     if (npc.Velocity.Y > num67)
                     {
                         npc.Velocity.Y = npc.Velocity.Y - num65 * 1.1f;
                     }
                 }
                 if ((double)(Math.Abs(npc.Velocity.X) + Math.Abs(npc.Velocity.Y)) < (double)num64 * 0.5)
                 {
                     if (npc.Velocity.X > 0f)
                     {
                         npc.Velocity.X = npc.Velocity.X + num65;
                     }
                     else
                     {
                         npc.Velocity.X = npc.Velocity.X - num65;
                     }
                 }
             }
         }
     }
     npc.rotation = (float)Math.Atan2((double)npc.Velocity.Y, (double)npc.Velocity.X) + 1.57f;
     return;
 }
        // 6 - 1.1.2
        private void AIWorm(NPC npc, bool flag, Func<Int32, Int32, ITile> TileRefs)
        {
            if (npc.type == NPCType.N117_LEECH_HEAD && npc.localAI[1] == 0f)
            {
                npc.localAI[1] = 1f;
            }
            if (npc.type >= NPCType.N13_EATER_OF_WORLDS_HEAD && npc.type <= NPCType.N15_EATER_OF_WORLDS_TAIL)
            {
                npc.realLife = -1;
            }
            else if (npc.ai[3] > 0f)
            {
                npc.realLife = (int)npc.ai[3];
            }

            if (npc.target < 0 || npc.target == 255 || Main.players[npc.target].dead)
            {
                npc.TargetClosest(true);
            }
            if (Main.players[npc.target].dead && npc.timeLeft > 300)
            {
                npc.timeLeft = 300;
            }

            if (npc.type == NPCType.N87_WYVERN_HEAD && npc.ai[0] == 0f)
            {
                npc.ai[3] = (float)npc.whoAmI;
                npc.realLife = npc.whoAmI;
                int num96 = npc.whoAmI;
                for (int num97 = 0; num97 < 14; num97++)
                {
                    int num98 = 89;
                    if (num97 == 1 || num97 == 8)
                    {
                        num98 = 88;
                    }
                    else if (num97 == 11)
                    {
                        num98 = 90;
                    }
                    else if (num97 == 12)
                    {
                        num98 = 91;
                    }
                    else if (num97 == 13)
                    {
                        num98 = 92;
                    }
                    int num99 = NewNPC((int)(npc.Position.X + (float)(npc.Width / 2)),
                        (int)(npc.Position.Y + (float)npc.Height), num98, npc.whoAmI, true);
                    Main.npcs[num99].ai[3] = (float)npc.whoAmI;
                    Main.npcs[num99].realLife = npc.whoAmI;
                    Main.npcs[num99].ai[1] = (float)num96;
                    Main.npcs[num96].ai[0] = (float)num99;
                    NetMessage.SendData(23, -1, -1, "", num99, 0f, 0f, 0f, 0);
                    num96 = num99;
                }
            }
            if ((npc.type == NPCType.N07_DEVOURER_HEAD || npc.type == NPCType.N08_DEVOURER_BODY || npc.type == NPCType.N10_GIANT_WORM_HEAD ||
                npc.type == NPCType.N11_GIANT_WORM_BODY || npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD || npc.type == NPCType.N14_EATER_OF_WORLDS_BODY ||
                npc.type == NPCType.N39_BONE_SERPENT_HEAD || npc.type == NPCType.N40_BONE_SERPENT_BODY || npc.type == NPCType.N95_DIGGER_HEAD ||
                npc.type == NPCType.N96_DIGGER_BODY || npc.type == NPCType.N98_SEEKER_HEAD || npc.type == NPCType.N99_SEEKER_BODY || npc.type == NPCType.N117_LEECH_HEAD ||
                npc.type == NPCType.N118_LEECH_BODY) && npc.ai[0] == 0f)
            {
                if (npc.type == NPCType.N07_DEVOURER_HEAD || npc.type == NPCType.N10_GIANT_WORM_HEAD || npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD ||
                    npc.type == NPCType.N39_BONE_SERPENT_HEAD || npc.type == NPCType.N95_DIGGER_HEAD || npc.type == NPCType.N98_SEEKER_HEAD ||
                    npc.type == NPCType.N117_LEECH_HEAD)
                {
                    if (npc.type < NPCType.N13_EATER_OF_WORLDS_HEAD || npc.type > NPCType.N15_EATER_OF_WORLDS_TAIL)
                    {
                        npc.ai[3] = (float)npc.whoAmI;
                        npc.realLife = npc.whoAmI;
                    }
                    npc.ai[2] = (float)Main.rand.Next(8, 13);
                    if (npc.type == NPCType.N10_GIANT_WORM_HEAD)
                    {
                        npc.ai[2] = (float)Main.rand.Next(4, 7);
                    }
                    if (npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD)
                    {
                        npc.ai[2] = (float)Main.rand.Next(45, 56);
                    }
                    if (npc.type == NPCType.N39_BONE_SERPENT_HEAD)
                    {
                        npc.ai[2] = (float)Main.rand.Next(12, 19);
                    }
                    if (npc.type == NPCType.N95_DIGGER_HEAD)
                    {
                        npc.ai[2] = (float)Main.rand.Next(6, 12);
                    }
                    if (npc.type == NPCType.N98_SEEKER_HEAD)
                    {
                        npc.ai[2] = (float)Main.rand.Next(20, 26);
                    }
                    if (npc.type == NPCType.N117_LEECH_HEAD)
                    {
                        npc.ai[2] = (float)Main.rand.Next(3, 6);
                    }
                    npc.ai[0] = (float)NewNPC((int)(npc.Position.X + (float)(npc.Width / 2)), (int)(npc.Position.Y + (float)npc.Height),
                        (int)npc.type + 1, npc.whoAmI, true);
                }
                else
                {
                    if ((npc.type == NPCType.N08_DEVOURER_BODY || npc.type == NPCType.N11_GIANT_WORM_BODY || npc.type == NPCType.N14_EATER_OF_WORLDS_BODY ||
                        npc.type == NPCType.N40_BONE_SERPENT_BODY || npc.type == NPCType.N96_DIGGER_BODY || npc.type == NPCType.N99_SEEKER_BODY ||
                        npc.type == NPCType.N118_LEECH_BODY) && npc.ai[2] > 0f)
                    {
                        npc.ai[0] = (float)NewNPC((int)(npc.Position.X + (float)(npc.Width / 2)), (int)(npc.Position.Y + (float)npc.Height),
                            (int)npc.type, npc.whoAmI, true);
                    }
                    else
                    {
                        npc.ai[0] = (float)NewNPC((int)(npc.Position.X + (float)(npc.Width / 2)), (int)(npc.Position.Y + (float)npc.Height),
                            (int)npc.type + 1, npc.whoAmI, true);
                    }
                }
                if (npc.type < NPCType.N13_EATER_OF_WORLDS_HEAD || npc.type > NPCType.N15_EATER_OF_WORLDS_TAIL)
                {
                    Main.npcs[(int)npc.ai[0]].ai[3] = npc.ai[3];
                    Main.npcs[(int)npc.ai[0]].realLife = npc.realLife;
                }
                Main.npcs[(int)npc.ai[0]].ai[1] = (float)npc.whoAmI;
                Main.npcs[(int)npc.ai[0]].ai[2] = npc.ai[2] - 1f;
                npc.netUpdate = true;
            }
            if ((npc.type == NPCType.N08_DEVOURER_BODY || npc.type == NPCType.N09_DEVOURER_TAIL || npc.type == NPCType.N11_GIANT_WORM_BODY ||
                npc.type == NPCType.N12_GIANT_WORM_TAIL || npc.type == NPCType.N40_BONE_SERPENT_BODY || npc.type == NPCType.N41_BONE_SERPENT_TAIL ||
                npc.type == NPCType.N96_DIGGER_BODY || npc.type == NPCType.N97_DIGGER_TAIL || npc.type == NPCType.N99_SEEKER_BODY || npc.type == NPCType.N100_SEEKER_TAIL ||
                (npc.type > NPCType.N87_WYVERN_HEAD && npc.type <= NPCType.N90_WYVERN_BODY_2) ||
                npc.type == NPCType.N118_LEECH_BODY || npc.type == NPCType.N119_LEECH_TAIL) &&
                (!Main.npcs[(int)npc.ai[1]].Active || Main.npcs[(int)npc.ai[1]].aiStyle != npc.aiStyle))
            {
                npc.life = 0;
                npc.HitEffect(0, 10.0);
                npc.Active = false;
            }
            if ((npc.type == NPCType.N07_DEVOURER_HEAD || npc.type == NPCType.N08_DEVOURER_BODY || npc.type == NPCType.N10_GIANT_WORM_HEAD ||
                npc.type == NPCType.N11_GIANT_WORM_BODY || npc.type == NPCType.N39_BONE_SERPENT_HEAD || npc.type == NPCType.N40_BONE_SERPENT_BODY ||
                npc.type == NPCType.N95_DIGGER_HEAD || npc.type == NPCType.N96_DIGGER_BODY || npc.type == NPCType.N98_SEEKER_HEAD || npc.type == NPCType.N99_SEEKER_BODY ||
                (npc.type >= NPCType.N87_WYVERN_HEAD && npc.type < NPCType.N92_WYVERN_TAIL) || npc.type == NPCType.N117_LEECH_HEAD || npc.type == NPCType.N118_LEECH_BODY) &&
                (!Main.npcs[(int)npc.ai[0]].Active || Main.npcs[(int)npc.ai[0]].aiStyle != npc.aiStyle))
            {
                npc.life = 0;
                npc.HitEffect(0, 10.0);
                npc.Active = false;
            }
            if (npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD || npc.type == NPCType.N14_EATER_OF_WORLDS_BODY || npc.type == NPCType.N15_EATER_OF_WORLDS_TAIL)
            {
                if (!Main.npcs[(int)npc.ai[1]].Active && !Main.npcs[(int)npc.ai[0]].Active)
                {
                    npc.life = 0;
                    npc.HitEffect(0, 10.0);
                    npc.Active = false;
                }
                if (npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD && !Main.npcs[(int)npc.ai[0]].Active)
                {
                    npc.life = 0;
                    npc.HitEffect(0, 10.0);
                    npc.Active = false;
                }
                if (npc.type == NPCType.N15_EATER_OF_WORLDS_TAIL && !Main.npcs[(int)npc.ai[1]].Active)
                {
                    npc.life = 0;
                    npc.HitEffect(0, 10.0);
                    npc.Active = false;
                }
                if (npc.type == NPCType.N14_EATER_OF_WORLDS_BODY && (!Main.npcs[(int)npc.ai[1]].Active || Main.npcs[(int)npc.ai[1]].aiStyle != npc.aiStyle))
                {
                    npc.type = NPCType.N13_EATER_OF_WORLDS_HEAD;
                    int num100 = npc.whoAmI;
                    float num101 = (float)npc.life / (float)npc.lifeMax;
                    float num102 = npc.ai[0];
                    npc.SetDefaults((int)npc.type);
                    npc.life = (int)((float)npc.lifeMax * num101);
                    npc.ai[0] = num102;
                    npc.TargetClosest(true);
                    npc.netUpdate = true;
                    npc.whoAmI = num100;
                }
                if (npc.type == NPCType.N14_EATER_OF_WORLDS_BODY && (!Main.npcs[(int)npc.ai[0]].Active || Main.npcs[(int)npc.ai[0]].aiStyle != npc.aiStyle))
                {
                    int num103 = npc.whoAmI;
                    float num104 = (float)npc.life / (float)npc.lifeMax;
                    float num105 = npc.ai[1];
                    npc.SetDefaults((int)npc.type);
                    npc.life = (int)((float)npc.lifeMax * num104);
                    npc.ai[1] = num105;
                    npc.TargetClosest(true);
                    npc.netUpdate = true;
                    npc.whoAmI = num103;
                }
                if (npc.life == 0)
                {
                    bool EoWAlive = false;
                    for (int npcId = 0; npcId < MAX_NPCS; npcId++)
                    {
                        var fNpc = Main.npcs[npcId];

                        if (fNpc.Active)
                        {
                            EoWAlive = (
                                fNpc.type == NPCType.N13_EATER_OF_WORLDS_HEAD ||
                                fNpc.type == NPCType.N14_EATER_OF_WORLDS_BODY ||
                                fNpc.type == NPCType.N15_EATER_OF_WORLDS_TAIL);

                            if (EoWAlive)
                                break;
                        }
                    }

                    if (!EoWAlive)
                    {
                        npc.boss = true;
                        npc.NPCLoot();
                    }
                }
            }
            if (!npc.Active)
            {
                NetMessage.SendData(28, -1, -1, "", npc.whoAmI, -1f, 0f, 0f, 0);
            }

            int num107 = (int)(npc.Position.X / 16f) - 1;
            int num108 = (int)((npc.Position.X + (float)npc.Width) / 16f) + 2;
            int num109 = (int)(npc.Position.Y / 16f) - 1;
            int num110 = (int)((npc.Position.Y + (float)npc.Height) / 16f) + 2;
            if (num107 < 0)
            {
                num107 = 0;
            }
            if (num108 > Main.maxTilesX)
            {
                num108 = Main.maxTilesX;
            }
            if (num109 < 0)
            {
                num109 = 0;
            }
            if (num110 > Main.maxTilesY)
            {
                num110 = Main.maxTilesY;
            }
            bool flag11 = false;
            if (npc.type >= NPCType.N87_WYVERN_HEAD && npc.type <= NPCType.N92_WYVERN_TAIL)
            {
                flag11 = true;
            }
            if (!flag11)
            {
                for (int num111 = num107; num111 < num108; num111++)
                {
                    for (int num112 = num109; num112 < num110; num112++)
                    {
                        if (((Main.tile.At(num111, num112).Active && (Main.tileSolid[(int)Main.tile.At(num111, num112).Type] ||
                            (Main.tileSolidTop[(int)Main.tile.At(num111, num112).Type] && Main.tile.At(num111, num112).FrameY == 0))) ||
                                Main.tile.At(num111, num112).Liquid > 64))
                        {
                            Vector2 vector13;
                            vector13.X = (float)(num111 * 16);
                            vector13.Y = (float)(num112 * 16);
                            if (npc.Position.X + (float)npc.Width > vector13.X && npc.Position.X < vector13.X + 16f && npc.Position.Y + (float)npc.Height > vector13.Y && npc.Position.Y < vector13.Y + 16f)
                            {
                                flag11 = true;
                                if (Main.rand.Next(100) == 0 && npc.type != NPCType.N117_LEECH_HEAD && Main.tile.At(num111, num112).Active)
                                {
                                    WorldModify.KillTile(TileRefs, null, num111, num112, true, true);
                                }
                            }
                        }
                    }
                }
            }
            if (!flag11 && (npc.type == NPCType.N07_DEVOURER_HEAD || npc.type == NPCType.N10_GIANT_WORM_HEAD || npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD ||
                npc.type == NPCType.N39_BONE_SERPENT_HEAD || npc.type == NPCType.N95_DIGGER_HEAD || npc.type == NPCType.N98_SEEKER_HEAD || npc.type == NPCType.N117_LEECH_HEAD))
            {
                Rectangle rectangle = new Rectangle((int)npc.Position.X, (int)npc.Position.Y, npc.Width, npc.Height);
                int num113 = 1000;
                bool flag12 = true;
                for (int num114 = 0; num114 < 255; num114++)
                {
                    if (Main.players[num114].Active)
                    {
                        Rectangle rectangle2 = new Rectangle((int)Main.players[num114].Position.X - num113, (int)Main.players[num114].Position.Y - num113, num113 * 2, num113 * 2);
                        if (rectangle.Intersects(rectangle2))
                        {
                            flag12 = false;
                            break;
                        }
                    }
                }
                if (flag12)
                {
                    flag11 = true;
                }
            }
            if (npc.type >= NPCType.N87_WYVERN_HEAD && npc.type <= NPCType.N92_WYVERN_TAIL)
            {
                if (npc.Velocity.X < 0f)
                {
                    npc.spriteDirection = 1;
                }
                else if (npc.Velocity.X > 0f)
                {
                    npc.spriteDirection = -1;
                }
            }
            float num115 = 8f;
            float num116 = 0.07f;
            if (npc.type == NPCType.N95_DIGGER_HEAD)
            {
                num115 = 5.5f;
                num116 = 0.045f;
            }
            if (npc.type == NPCType.N10_GIANT_WORM_HEAD)
            {
                num115 = 6f;
                num116 = 0.05f;
            }
            if (npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD)
            {
                num115 = 10f;
                num116 = 0.07f;
            }
            if (npc.type == NPCType.N87_WYVERN_HEAD)
            {
                num115 = 11f;
                num116 = 0.25f;
            }
            if (npc.type == NPCType.N117_LEECH_HEAD && Main.WallOfFlesh >= 0)
            {
                float num117 = (float)Main.npcs[Main.WallOfFlesh].life / (float)Main.npcs[Main.WallOfFlesh].lifeMax;
                if ((double)num117 < 0.5)
                {
                    num115 += 1f;
                    num116 += 0.1f;
                }
                if ((double)num117 < 0.25)
                {
                    num115 += 1f;
                    num116 += 0.1f;
                }
                if ((double)num117 < 0.1)
                {
                    num115 += 2f;
                    num116 += 0.1f;
                }
            }
            Vector2 vector14 = new Vector2(npc.Position.X + (float)npc.Width * 0.5f, npc.Position.Y + (float)npc.Height * 0.5f);
            float num118 = Main.players[npc.target].Position.X + (float)(Main.players[npc.target].Width / 2);
            float num119 = Main.players[npc.target].Position.Y + (float)(Main.players[npc.target].Height / 2);
            num118 = (float)((int)(num118 / 16f) * 16);
            num119 = (float)((int)(num119 / 16f) * 16);
            vector14.X = (float)((int)(vector14.X / 16f) * 16);
            vector14.Y = (float)((int)(vector14.Y / 16f) * 16);
            num118 -= vector14.X;
            num119 -= vector14.Y;
            float num120 = (float)Math.Sqrt((double)(num118 * num118 + num119 * num119));
            if (npc.ai[1] > 0f && npc.ai[1] < (float)Main.npcs.Length)
            {
                try
                {
                    vector14 = new Vector2(npc.Position.X + (float)npc.Width * 0.5f, npc.Position.Y + (float)npc.Height * 0.5f);
                    num118 = Main.npcs[(int)npc.ai[1]].Position.X + (float)(Main.npcs[(int)npc.ai[1]].Width / 2) - vector14.X;
                    num119 = Main.npcs[(int)npc.ai[1]].Position.Y + (float)(Main.npcs[(int)npc.ai[1]].Height / 2) - vector14.Y;
                }
                catch
                { }

                npc.rotation = (float)Math.Atan2((double)num119, (double)num118) + 1.57f;
                num120 = (float)Math.Sqrt((double)(num118 * num118 + num119 * num119));
                int num121 = npc.Width;
                if (npc.type >= NPCType.N87_WYVERN_HEAD && npc.type <= NPCType.N92_WYVERN_TAIL)
                {
                    num121 = 42;
                }
                num120 = (num120 - (float)num121) / num120;
                num118 *= num120;
                num119 *= num120;
                npc.Velocity = default(Vector2);
                npc.Position.X = npc.Position.X + num118;
                npc.Position.Y = npc.Position.Y + num119;
                if (npc.type >= NPCType.N87_WYVERN_HEAD && npc.type <= NPCType.N92_WYVERN_TAIL)
                {
                    if (num118 < 0f)
                    {
                        npc.spriteDirection = 1;
                        return;
                    }
                    if (num118 > 0f)
                    {
                        npc.spriteDirection = -1;
                        return;
                    }
                }
            }
            else if (!flag11)
            {
                npc.TargetClosest(true);
                npc.Velocity.Y = npc.Velocity.Y + 0.11f;
                if (npc.Velocity.Y > num115)
                {
                    npc.Velocity.Y = num115;
                }
                if ((double)(Math.Abs(npc.Velocity.X) + Math.Abs(npc.Velocity.Y)) < (double)num115 * 0.4)
                {
                    if (npc.Velocity.X < 0f)
                    {
                        npc.Velocity.X = npc.Velocity.X - num116 * 1.1f;
                    }
                    else
                    {
                        npc.Velocity.X = npc.Velocity.X + num116 * 1.1f;
                    }
                }
                else if (npc.Velocity.Y == num115)
                {
                    if (npc.Velocity.X < num118)
                    {
                        npc.Velocity.X = npc.Velocity.X + num116;
                    }
                    else if (npc.Velocity.X > num118)
                    {
                        npc.Velocity.X = npc.Velocity.X - num116;
                    }
                }
                else if (npc.Velocity.Y > 4f)
                {
                    if (npc.Velocity.X < 0f)
                    {
                        npc.Velocity.X = npc.Velocity.X + num116 * 0.9f;
                    }
                    else
                    {
                        npc.Velocity.X = npc.Velocity.X - num116 * 0.9f;
                    }
                }
            }
            else
            {
                if (npc.type != NPCType.N87_WYVERN_HEAD && npc.type != NPCType.N117_LEECH_HEAD && npc.soundDelay == 0)
                {
                    float num122 = num120 / 40f;
                    if (num122 < 10f)
                    {
                        num122 = 10f;
                    }
                    if (num122 > 20f)
                    {
                        num122 = 20f;
                    }
                    npc.soundDelay = (int)num122;
                }
                num120 = (float)Math.Sqrt((double)(num118 * num118 + num119 * num119));
                float num123 = Math.Abs(num118);
                float num124 = Math.Abs(num119);
                float num125 = num115 / num120;
                num118 *= num125;
                num119 *= num125;
                if ((npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD || npc.type == NPCType.N07_DEVOURER_HEAD) && !Main.players[npc.target].zoneEvil)
                {
                    bool flag13 = true;
                    for (int num126 = 0; num126 < 255; num126++)
                    {
                        if (Main.players[num126].Active && !Main.players[num126].dead && Main.players[num126].zoneEvil)
                        {
                            flag13 = false;
                        }
                    }
                    if (flag13 & !npc.MadeSpawn)
                    {
                        if ((double)(npc.Position.Y / 16f) > (Main.rockLayer + (double)Main.maxTilesY) / 2.0)
                        {
                            npc.Active = false;
                            int num127 = (int)npc.ai[0];
                            while (num127 > 0 && num127 < 200 && Main.npcs[num127].Active && Main.npcs[num127].aiStyle == npc.aiStyle)
                            {
                                int num128 = (int)Main.npcs[num127].ai[0];
                                Main.npcs[num127].Active = false;
                                npc.life = 0;

                                NetMessage.SendData(23, -1, -1, "", num127);

                                num127 = num128;
                            }

                            NetMessage.SendData(23, -1, -1, "", npc.whoAmI);
                        }
                        num118 = 0f;
                        num119 = num115;
                    }
                }
                bool flag14 = false;
                if (npc.type == NPCType.N87_WYVERN_HEAD)
                {
                    if (((npc.Velocity.X > 0f && num118 < 0f) || (npc.Velocity.X < 0f && num118 > 0f) || (npc.Velocity.Y > 0f && num119 < 0f) || (npc.Velocity.Y < 0f && num119 > 0f)) && Math.Abs(npc.Velocity.X) + Math.Abs(npc.Velocity.Y) > num116 / 2f && num120 < 300f)
                    {
                        flag14 = true;
                        if (Math.Abs(npc.Velocity.X) + Math.Abs(npc.Velocity.Y) < num115)
                        {
                            npc.Velocity *= 1.1f;
                        }
                    }
                    if (npc.Position.Y > Main.players[npc.target].Position.Y || (double)(Main.players[npc.target].Position.Y / 16f) > Main.worldSurface || Main.players[npc.target].dead)
                    {
                        flag14 = true;
                        if (Math.Abs(npc.Velocity.X) < num115 / 2f)
                        {
                            if (npc.Velocity.X == 0f)
                            {
                                npc.Velocity.X = npc.Velocity.X - (float)npc.direction;
                            }
                            npc.Velocity.X = npc.Velocity.X * 1.1f;
                        }
                        else if (npc.Velocity.Y > -num115)
                        {
                            npc.Velocity.Y = npc.Velocity.Y - num116;
                        }
                    }
                }
                if (!flag14)
                {
                    if ((npc.Velocity.X > 0f && num118 > 0f) || (npc.Velocity.X < 0f && num118 < 0f) || (npc.Velocity.Y > 0f && num119 > 0f) || (npc.Velocity.Y < 0f && num119 < 0f))
                    {
                        if (npc.Velocity.X < num118)
                        {
                            npc.Velocity.X = npc.Velocity.X + num116;
                        }
                        else if (npc.Velocity.X > num118)
                        {
                            npc.Velocity.X = npc.Velocity.X - num116;
                        }

                        if (npc.Velocity.Y < num119)
                        {
                            npc.Velocity.Y = npc.Velocity.Y + num116;
                        }
                        else if (npc.Velocity.Y > num119)
                        {
                            npc.Velocity.Y = npc.Velocity.Y - num116;
                        }

                        if ((double)Math.Abs(num119) < (double)num115 * 0.2 && ((npc.Velocity.X > 0f && num118 < 0f) || (npc.Velocity.X < 0f && num118 > 0f)))
                        {
                            if (npc.Velocity.Y > 0f)
                            {
                                npc.Velocity.Y = npc.Velocity.Y + num116 * 2f;
                            }
                            else
                            {
                                npc.Velocity.Y = npc.Velocity.Y - num116 * 2f;
                            }
                        }
                        if ((double)Math.Abs(num118) < (double)num115 * 0.2 && ((npc.Velocity.Y > 0f && num119 < 0f) || (npc.Velocity.Y < 0f && num119 > 0f)))
                        {
                            if (npc.Velocity.X > 0f)
                            {
                                npc.Velocity.X = npc.Velocity.X + num116 * 2f;
                            }
                            else
                            {
                                npc.Velocity.X = npc.Velocity.X - num116 * 2f;
                            }
                        }
                    }
                    else if (num123 > num124)
                    {
                        if (npc.Velocity.X < num118)
                        {
                            npc.Velocity.X = npc.Velocity.X + num116 * 1.1f;
                        }
                        else if (npc.Velocity.X > num118)
                        {
                            npc.Velocity.X = npc.Velocity.X - num116 * 1.1f;
                        }
                        if ((double)(Math.Abs(npc.Velocity.X) + Math.Abs(npc.Velocity.Y)) < (double)num115 * 0.5)
                        {
                            if (npc.Velocity.Y > 0f)
                            {
                                npc.Velocity.Y = npc.Velocity.Y + num116;
                            }
                            else
                            {
                                npc.Velocity.Y = npc.Velocity.Y - num116;
                            }
                        }
                    }
                    else
                    {
                        if (npc.Velocity.Y < num119)
                        {
                            npc.Velocity.Y = npc.Velocity.Y + num116 * 1.1f;
                        }
                        else if (npc.Velocity.Y > num119)
                        {
                            npc.Velocity.Y = npc.Velocity.Y - num116 * 1.1f;
                        }

                        if ((double)(Math.Abs(npc.Velocity.X) + Math.Abs(npc.Velocity.Y)) < (double)num115 * 0.5)
                        {
                            if (npc.Velocity.X > 0f)
                            {
                                npc.Velocity.X = npc.Velocity.X + num116;
                            }
                            else
                            {
                                npc.Velocity.X = npc.Velocity.X - num116;
                            }
                        }
                    }
                }
            }

            npc.rotation = (float)Math.Atan2((double)npc.Velocity.Y, (double)npc.Velocity.X) + 1.57f;
            if (npc.type == NPCType.N07_DEVOURER_HEAD || npc.type == NPCType.N10_GIANT_WORM_HEAD ||
                npc.type == NPCType.N13_EATER_OF_WORLDS_HEAD || npc.type == NPCType.N39_BONE_SERPENT_HEAD || npc.type == NPCType.N95_DIGGER_HEAD ||
                npc.type == NPCType.N98_SEEKER_HEAD || npc.type == NPCType.N117_LEECH_HEAD)
            {
                if (flag11)
                {
                    if (npc.localAI[0] != 1f)
                    {
                        npc.netUpdate = true;
                    }
                    npc.localAI[0] = 1f;
                }
                else
                {
                    if (npc.localAI[0] != 0f)
                    {
                        npc.netUpdate = true;
                    }
                    npc.localAI[0] = 0f;
                }
                if (((npc.Velocity.X > 0f && npc.oldVelocity.X < 0f) || (npc.Velocity.X < 0f && npc.oldVelocity.X > 0f) || (npc.Velocity.Y > 0f && npc.oldVelocity.Y < 0f) || (npc.Velocity.Y < 0f && npc.oldVelocity.Y > 0f)) && !npc.justHit)
                {
                    npc.netUpdate = true;
                    return;
                }
            }
        }