private List <ClientTile> ReadFloorDescription(NetworkMessage nmsg, ref int skipTiles, int startX, int startY, int Z, int width, int height, int offset)
        {
            List <ClientTile> tiles = new List <ClientTile>();

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    if (skipTiles > 0)
                    {
                        skipTiles--;
                    }
                    else
                    {
                        MapPosition pos  = new MapPosition(startX + x + offset, startY + y + offset, Z);
                        ClientTile  Tile = ReadTileDescription(nmsg, pos);
                        skipTiles = nmsg.ReadByte();
                        if (nmsg.ReadByte() != 0xFF)
                        {
                            Log.Warning("Server did not follow Tile skip by 0xFF");
                        }
                        tiles.Add(Tile);
                    }
                }
            }

            return(tiles);
        }
        public void parsePacket(NetworkMessage nmsg)
        {
            int packetType = nmsg.ReadByte();

            if (PacketParsers.ContainsKey(packetType))
            {
                PacketParser  pp      = PacketParsers[packetType];
                Packet        p       = pp.parser(nmsg);
                ProtocolEvent handler = PacketHandlers[packetType];
                if (handler != null && p != null)
                {
                    handler.Invoke(p);
                }

                /*
                 * string m = "Parsing packet 0x" + packetType.ToString("X2") + " '" + ph.name + "'";
                 * if (p != null)
                 *  m += "<pre>" + p.ToString() + "</pre>";
                 * Log.Debug(m);
                 */
                if (!nmsg.ReadAllData())
                {
                    parsePacket(nmsg);
                }
            }
            else
            {
                Log.Warning("Unknown packet type parsed 0x" + packetType.ToString("X2"), this);
            }
        }
        /// <summary>
        /// Reads a position (x, y, z) from the message
        /// </summary>
        /// <param name="nmsg">The message to read from</param>
        /// <returns></returns>
        private MapPosition ReadMapPosition(NetworkMessage nmsg)
        {
            int x = nmsg.ReadU16();
            int y = nmsg.ReadU16();
            int z = nmsg.ReadByte();

            return(new MapPosition(x, y, z));
        }
        /// <summary>
        /// Read an Item and returns it.
        /// </summary>
        /// <param name="nmsg">Packet to read from.</param>
        /// <param name="clientID">If not 0, the function will not read client ID from the packet but use this one instead.</param>
        /// <returns></returns>
        private ClientItem ReadItem(NetworkMessage nmsg, UInt16 clientID = 0)
        {
            if (clientID == 0)
            {
                clientID = nmsg.ReadU16();
            }
            int      subtype = -1;
            ItemType it      = GameData.GetItemType(clientID);

            if (it == null)
            {
                Log.Warning("Item packet contains unrecognizabe item (" + clientID + ")");
                it = ItemType.NullType;
            }

            if (it.IsStackable || it.IsFluidContainer || it.IsSplash)
            {
                subtype = nmsg.ReadByte();
            }

            return(new ClientItem(it, subtype));
        }
        public void parsePacket(NetworkMessage nmsg)
        {
            int packetType = nmsg.ReadByte();

            if (PacketParsers.ContainsKey(packetType))
            {
                PacketParser pp = PacketParsers[packetType];
                Packet p = pp.parser(nmsg);
                ProtocolEvent handler = PacketHandlers[packetType];
                if (handler != null && p != null)
                    handler.Invoke(p);
                /*
                string m = "Parsing packet 0x" + packetType.ToString("X2") + " '" + ph.name + "'";
                if (p != null)
                    m += "<pre>" + p.ToString() + "</pre>";
                Log.Debug(m);
                 */
                if (!nmsg.ReadAllData())
                    parsePacket(nmsg);
            }
            else
            {
                Log.Warning("Unknown packet type parsed 0x" + packetType.ToString("X2"), this);
            }
        }
 /// <summary>
 /// Reads a position (x, y, z) from the message
 /// </summary>
 /// <param name="nmsg">The message to read from</param>
 /// <returns></returns>
 private MapPosition ReadMapPosition(NetworkMessage nmsg)
 {
     int x = nmsg.ReadU16();
     int y = nmsg.ReadU16();
     int z = nmsg.ReadByte();
     return new MapPosition(x, y, z);
 }
        /// <summary>
        /// Read an Item and returns it.
        /// </summary>
        /// <param name="nmsg">Packet to read from.</param>
        /// <param name="clientID">If not 0, the function will not read client ID from the packet but use this one instead.</param>
        /// <returns></returns>
        private ClientItem ReadItem(NetworkMessage nmsg, UInt16 clientID = 0)
        {
            if (clientID == 0)
                clientID = nmsg.ReadU16();
            int subtype = -1;
            ItemType it = GameData.GetItemType(clientID);
            if (it == null)
            {
                Log.Warning("Item packet contains unrecognizabe item (" + clientID + ")");
                it = ItemType.NullType;
            }

            if (it.IsStackable || it.IsFluidContainer || it.IsSplash)
                subtype = nmsg.ReadByte();

            return new ClientItem(it, subtype);
        }
        private List<ClientTile> ReadFloorDescription(NetworkMessage nmsg, ref int skipTiles, int startX, int startY, int Z, int width, int height, int offset)
        {
            List<ClientTile> tiles = new List<ClientTile>();
            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    if (skipTiles > 0)
                        skipTiles--;
                    else
                    {
                        MapPosition pos = new MapPosition(startX + x + offset, startY + y + offset, Z);
                        ClientTile Tile = ReadTileDescription(nmsg, pos);
                        skipTiles = nmsg.ReadByte();
                        if (nmsg.ReadByte() != 0xFF)
                            Log.Warning("Server did not follow Tile skip by 0xFF");
                        tiles.Add(Tile);
                    }
                }
            }

            return tiles;
        }
        /// <summary>
        /// Reads a Creature and returns it. This includes Outfit & Light etc.
        /// </summary>
        /// <param name="nmsg">Message to read from.</param>
        /// <param name="props">The read properties will be inserted into this property object.</param>
        /// <returns></returns>
        private ClientCreature ReadCreature(NetworkMessage nmsg, UInt32 CreatureID)
        {
            ClientCreature Creature = null;
            if (CreatureID == Player.ID)
                Creature = Player;
            else if (!KnownCreatures.TryGetValue(CreatureID, out Creature))
            {
                Creature = new ClientCreature(CreatureID);
                KnownCreatures.Add(CreatureID, Creature);
            }
            else
            {
                ;
            }

            Creature.Health = nmsg.ReadByte();
            Creature.Direction = (Direction)nmsg.ReadByte();

            // TODO: This is U16 for later versions
            Creature.Outfit.LookType = (int)nmsg.ReadByte();
            if (Creature.Outfit.LookType == 0)
            {
                // looktypeEx
                Creature.Outfit.LookItem = nmsg.ReadU16();
            }
            else
            {
                Creature.Outfit.LookHead = nmsg.ReadByte();
                Creature.Outfit.LookBody = nmsg.ReadByte();
                Creature.Outfit.LookLegs = nmsg.ReadByte();
                Creature.Outfit.LookFeet = nmsg.ReadByte();
            }

            Creature.Light.Level = nmsg.ReadByte();
            Creature.Light.Color = nmsg.ReadByte();

            Creature.Speed = nmsg.ReadU16();

            Creature.Skull = (CreatureSkull)nmsg.ReadByte();
            Creature.Shield = (PartyShield)nmsg.ReadByte();

            return Creature;
        }
        /// <summary>
        /// Reads a Creature and returns it. This includes Outfit & Light etc.
        /// </summary>
        /// <param name="nmsg">Message to read from.</param>
        /// <param name="props">The read properties will be inserted into this property object.</param>
        /// <returns></returns>
        private ClientCreature ReadCreature(NetworkMessage nmsg, UInt32 CreatureID)
        {
            ClientCreature Creature = null;

            if (CreatureID == Player.ID)
            {
                Creature = Player;
            }
            else if (!KnownCreatures.TryGetValue(CreatureID, out Creature))
            {
                Creature = new ClientCreature(CreatureID);
                KnownCreatures.Add(CreatureID, Creature);
            }
            else
            {
                ;
            }

            Creature.Health    = nmsg.ReadByte();
            Creature.Direction = (Direction)nmsg.ReadByte();

            // TODO: This is U16 for later versions
            Creature.Outfit.LookType = (int)nmsg.ReadByte();
            if (Creature.Outfit.LookType == 0)
            {
                // looktypeEx
                Creature.Outfit.LookItem = nmsg.ReadU16();
            }
            else
            {
                Creature.Outfit.LookHead = nmsg.ReadByte();
                Creature.Outfit.LookBody = nmsg.ReadByte();
                Creature.Outfit.LookLegs = nmsg.ReadByte();
                Creature.Outfit.LookFeet = nmsg.ReadByte();
            }

            Creature.Light.Level = nmsg.ReadByte();
            Creature.Light.Color = nmsg.ReadByte();

            Creature.Speed = nmsg.ReadU16();

            Creature.Skull  = (CreatureSkull)nmsg.ReadByte();
            Creature.Shield = (PartyShield)nmsg.ReadByte();

            return(Creature);
        }