// Call this _only_ on server startup!
        /// <summary>
        /// Reads all NPCs from database and adds them to servers list.
        /// </summary>
        /// <returns>Number of NPCs loaded</returns>
        public static int CacheAllFromDB()
        {
            int npcCount = 0;
            SqlWrapper sqlWrapper = new SqlWrapper();

            // TODO:COUNT
            string sql = "SELECT count(*) FROM `mobspawns`";
            int numberOfNpc = sqlWrapper.SqlCount(sql);

            Console.Write("Reading spawns: 0/" + numberOfNpc.ToString());
            sql = "SELECT * FROM `mobspawns`";
            DataTable dt = sqlWrapper.ReadDatatable(sql);
            sqlWrapper = new SqlWrapper();
            DataTable dtstats = sqlWrapper.ReadDatatable("SELECT * from mobspawns_stats ORDER BY id, stat ASC");
            sqlWrapper = new SqlWrapper();
            DataTable dtinventory =
                sqlWrapper.ReadDatatable("SELECT * from mobspawnsinventory order by id, placement ASC");
            int statcount = 0;
            int invcount = 0;
            foreach (DataRow row in dt.Rows)
            {
                NonPlayerCharacterClass monster = new NonPlayerCharacterClass(0, 0)
                    { Starting = true, Id = (Int32)row["ID"], PlayField = (Int32)row["Playfield"] };

                monster.Name = (string)row["Name"]
#if DEBUG
                               + " " + monster.Id.ToString() // ID is for debug purpose only
#endif
                    ;
                monster.readcoordsheadingfast(row);
                statcount = monster.ReadStatsfast(dtstats, statcount);
                invcount = monster.readInventoryfromSqlfast(dtinventory, invcount);
                //                mMonster.readMeshsfromSql();
                //                mMonster.readNanosfromSql();
                //                mMonster.readTimersfromSql();
                //                mMonster.readWaypointsfromSql();
                //                mMonster.readWeaponpairsfromSql();

                monster.readTexturesfromSqlfast(row);
                byte[] bytes;
                long counter;
                if (!(row[15] is DBNull))
                {
                    bytes = (byte[])row[15]; // Waypoints
                    counter = 0;
                    while (counter < bytes.Length)
                    {
                        AOCoord aoCoord = new AOCoord();
                        aoCoord.x = BitConverter.ToSingle(bytes, (int)counter);
                        counter += 4;
                        aoCoord.y = BitConverter.ToSingle(bytes, (int)counter);
                        counter += 4;
                        aoCoord.z = BitConverter.ToSingle(bytes, (int)counter);
                        counter += 4;
                        monster.Waypoints.Add(aoCoord);
                    }
                }

                if (!(row[16] is DBNull))
                {
                    bytes = (byte[])row[16]; // Weaponpairs
                    counter = 0;
                    while (counter < bytes.Length)
                    {
                        AOWeaponpairs tempWeaponpairs = new AOWeaponpairs();
                        tempWeaponpairs.value1 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempWeaponpairs.value2 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempWeaponpairs.value3 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempWeaponpairs.value4 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        monster.Weaponpairs.Add(tempWeaponpairs);
                    }
                }

                if (!(row[17] is DBNull))
                {
                    bytes = (byte[])row[17]; // Running Nanos
                    counter = 0;
                    while (counter < bytes.Length)
                    {
                        AONano tempNano = new AONano();

                        tempNano.Nanotype = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempNano.Instance = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempNano.Value3 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempNano.Time1 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempNano.Time2 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        monster.ActiveNanos.Add(tempNano);
                    }
                }

                if (!(row[18] is DBNull))
                {
                    counter = 0;
                    bytes = (byte[])row[18]; // Meshs
                    while (counter < bytes.Length)
                    {
                        AOMeshs tempMeshs = new AOMeshs();
                        tempMeshs.Position = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempMeshs.Mesh = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempMeshs.OverrideTexture = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;

                        monster.Meshs.Add(tempMeshs);
                    }
                }

                if (!(row[19] is DBNull))
                {
                    counter = 0;
                    bytes = (byte[])row[19]; // Additional Meshs
                    while (counter < bytes.Length)
                    {
                        AOAddMeshs tempAdditionalMeshs = new AOAddMeshs();
                        tempAdditionalMeshs.position = bytes[counter++];
                        tempAdditionalMeshs.meshvalue1 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempAdditionalMeshs.meshvalue2 = BitConverter.ToInt32(bytes, (int)counter);
                        counter += 4;
                        tempAdditionalMeshs.priority = bytes[counter++];

                        monster.AdditionalMeshs.Add(tempAdditionalMeshs);
                    }
                }
                monster.Starting = false;

                Program.zoneServer.Monsters.Add(monster);
                npcCount += 1;
                if ((npcCount % 100) == 0)
                {
                    Console.Write("\rReading spawns: " + npcCount.ToString() + "/" + numberOfNpc.ToString());
                }
            }

            Console.Write("\r                                                    \r");
            return npcCount;
        }
        public static List<AOMeshs> GetMeshs(Character character, bool showsocial, bool socialonly)
        {
            List<AOMeshs> meshs;
            List<AOMeshs> socials;
            List<AOMeshs> output = new List<AOMeshs>();

            bool leftPadVisible;
            bool rightPadVisible;
            bool doubleLeftPad;
            bool doubleRightPad;

            lock (character)
            {
                meshs = character.MeshLayer.GetMeshs();
                socials = character.SocialMeshLayer.GetMeshs();

                int visualFlags = character.Stats.VisualFlags.Value;
                rightPadVisible = ((visualFlags & 0x1) > 0);
                leftPadVisible = ((visualFlags & 0x2) > 0);
                bool showHelmet = ((visualFlags & 0x4) > 0);
                doubleLeftPad = ((visualFlags & 0x8) > 0);
                doubleRightPad = ((visualFlags & 0x10) > 0);

                if (!showHelmet)
                {
                    if (meshs.ElementAt(0).Position == 0) // Helmet there?
                        // This probably needs to be looked at (glasses/visors)
                    {
                        if (meshs.ElementAt(0).Mesh != character.Stats.HeadMesh.StatBaseValue) // Dont remove the head :)
                        {
                            meshs.RemoveAt(0);
                        }
                    }

                    if (socials.ElementAt(0).Position == 0) // Helmet there?
                        // This probably needs to be looked at (glasses/visors)
                    {
                        if (socials.ElementAt(0).Mesh != character.Stats.HeadMesh.StatBaseValue) // Dont remove the head :)
                        {
                            socials.RemoveAt(0);
                        }
                    }
                }
            }

            socialonly &= showsocial; // Disable socialonly flag if showsocial is false

            // preapply visual flags

            #region Applying visual flags
            if (socialonly)
            {
                meshs.Clear();
            }
            if ((!socialonly) && (!showsocial))
            {
                socials.Clear();
            }

            AOMeshs leftShoulder = null;
            AOMeshs rightShoulder = null;
            int rightShoulderNum = -1;
            int leftShoulderNum = -1;

            for (int counter1 = 0; counter1 < meshs.Count; counter1++)
            {
                AOMeshs m = meshs.ElementAt(counter1);
                if (m.Position == 3)
                {
                    if (!rightPadVisible)
                    {
                        meshs.RemoveAt(counter1);
                        counter1--;
                        continue;
                    }
                    else
                    {
                        rightShoulder = m;
                        rightShoulderNum = counter1;
                    }
                }
                if (m.Position == 4)
                {
                    if (!leftPadVisible)
                    {
                        meshs.RemoveAt(counter1);
                        counter1--;
                        continue;
                    }
                    else
                    {
                        leftShoulderNum = counter1;
                        leftShoulder = m;
                    }
                }
            }
            #endregion

            int counter;
            for (counter = 0; counter < 7; counter++)
            {
                AOMeshs cloth = null;
                AOMeshs social = null;

                foreach (AOMeshs aoMeshs in meshs)
                {
                    if (aoMeshs.Position == counter)
                    {
                        cloth = aoMeshs;
                        break;
                    }
                }

                foreach (AOMeshs aoMeshs in socials)
                {
                    if (aoMeshs.Position == counter)
                    {
                        social = aoMeshs;
                    }
                }

                if (social != null)
                {
                    if ((cloth != null) && (social != null))
                    {
                        // Compare layer only when both slots are set
                        if (cloth.Position == 0)
                        {
                            if (social.Mesh != character.Stats.HeadMesh.StatBaseValue)
                            {
                                cloth = social;
                            }
                        }
                        else if (social.Layer - 8 < cloth.Layer)
                        {
                            if (cloth.Position == 5) // Backmeshs act different
                            {
                                output.Add(cloth);
                            }
                            cloth = social;
                        }
                    }
                    else
                    {
                        if ((showsocial) || (socialonly))
                        {
                            cloth = social;
                        }
                    }
                }
                if (cloth != null)
                {
                    output.Add(cloth);

                    // Moved check for Double pads here
                    if ((cloth.Position == 3) && doubleRightPad)
                    {
                        AOMeshs temp = new AOMeshs();
                        temp.Position = 4;
                        temp.Layer = cloth.Layer;
                        temp.Mesh = cloth.Mesh;
                        temp.OverrideTexture = cloth.OverrideTexture;
                        output.Add(temp);
                    }

                    if ((cloth.Position == 4) && doubleLeftPad)
                    {
                        AOMeshs temp = new AOMeshs();
                        temp.Position = 3;
                        temp.Layer = cloth.Layer;
                        temp.Mesh = cloth.Mesh;
                        temp.OverrideTexture = cloth.OverrideTexture;
                        output.Add(temp);
                    }
                }
            }
            return output;
        }
 public List<AOMeshs> GetMeshs()
 {
     List<AOMeshs> meshList = new List<AOMeshs>();
     int counter;
     for (counter = 0; counter < this.mesh.Count; counter++)
     {
         AOMeshs aoMesh = new AOMeshs();
         aoMesh.Position = this.mesh.ElementAt(counter).Key >> 16;
         aoMesh.Mesh = this.mesh.ElementAt(counter).Value;
         aoMesh.OverrideTexture = this.meshOverride.ElementAt(counter).Value;
         aoMesh.Layer = this.mesh.ElementAt(counter).Key & 0xffff;
         meshList.Add(aoMesh);
     }
     return meshList;
 }
 public AOMeshs GetMeshAtPosition(int pos)
 {
     foreach (int key in this.mesh.Keys)
     {
         if ((key >> 16) == pos)
         {
             // Just return mesh with highest priority (0 = highest)
             AOMeshs aoMeshs = new AOMeshs();
             aoMeshs.Layer = key & 0xffff;
             aoMeshs.Position = key >> 16;
             aoMeshs.Mesh = this.mesh[key];
             aoMeshs.OverrideTexture = this.meshOverride[key];
             return aoMeshs;
         }
     }
     // No mesh at this position found
     return null;
 }
        public void readMeshsfromSQL()
        {
            SqlWrapper Sql = new SqlWrapper();
            Meshs.Clear();
            AOMeshs m_m;
            DataTable dt = Sql.ReadDT("SELECT * from " + getSQLTablefromDynelType() + "meshs WHERE ID=" + ID.ToString() + " AND playfield=" + PlayField.ToString());

            foreach (DataRow row in dt.Rows)
            {
                m_m = new AOMeshs();
                m_m.Position = (Int32)row["meshvalue1"];
                m_m.Mesh = (Int32)row["meshvalue2"];
                m_m.OverrideTexture = (Int32)row["meshvalue3"];
            }
        }