Exemplo n.º 1
0
        private static bool ParseTileArea(NodeStruct node, FileLoader fileLoader)
        {
            byte type;

            MemoryStream props;

            if (!fileLoader.GetProps(node, out props))
            {
                return(false);
            }

            int baseX = props.ReadUInt16();
            int baseY = props.ReadUInt16();
            int baseZ = props.ReadByte();

            NodeStruct nodeTile = fileLoader.GetChildNode(node, out type);

            while (nodeTile != null)
            {
                if (type != (byte)OtbmNodeTypes.Tile && type != (byte)OtbmNodeTypes.HouseTile)
                {
                    Logger.Log(LogLevels.Error, "NOT TILE OR HOUSETILE");
                    return(false);
                }

                if (!fileLoader.GetProps(nodeTile, out props))
                {
                    return(false);
                }

                int pX = baseX + props.ReadByte();
                int pY = baseY + props.ReadByte();
                int pZ = baseZ;

                TileFlags tileFlags   = TileFlags.None;
                bool      isHouseTile = false;
                Item      groundItem  = null;
                House     house       = null;
                Tile      tile        = null;

                if (type == (byte)OtbmNodeTypes.HouseTile)
                {
                    //TODO: HOUSES
                    uint houseId = props.ReadUInt32();

                    if (!Houses.ContainsKey(houseId))
                    {
                        Houses.Add(houseId, new House {
                            HouseId = houseId
                        });
                    }

                    house = Houses[houseId];
                    tile  = new HouseTile(pX, pY, pZ, house);
                    house.AddTile((HouseTile)tile);
                    isHouseTile = true;
                }

                int attribute;
                while ((attribute = props.ReadByte()) != -1)
                {
                    switch ((OtbmAttributes)attribute)
                    {
                    case OtbmAttributes.TileFlags:
                        TileFlags flags = (TileFlags)props.ReadUInt32();

                        if (flags.HasFlag(TileFlags.ProtectionZone))
                        {
                            tileFlags |= TileFlags.ProtectionZone;
                        }
                        else if (flags.HasFlag(TileFlags.NoPvpZone))
                        {
                            tileFlags |= TileFlags.NoPvpZone;
                        }
                        else if (flags.HasFlag(TileFlags.PvpZone))
                        {
                            tileFlags |= TileFlags.PvpZone;
                        }

                        if (flags.HasFlag(TileFlags.NoLogout))
                        {
                            tileFlags |= TileFlags.NoLogout;
                        }
                        break;

                    case OtbmAttributes.Item:
                        Item item = Item.CreateItem(props);
                        if (item == null)
                        {
                            return(false);
                        }

                        if (isHouseTile && item.IsMovable)
                        {
                            Logger.Log(LogLevels.Warning, "Moveable item found in house: " + house.HouseId);
                            item.DecrementReferenceCounter();
                        }
                        else
                        {
                            if (item.Count <= 0)
                            {
                                item.Count = 1;
                            }

                            if (tile != null)
                            {
                                tile.InternalAddThing(item);
                                item.StartDecaying();
                                item.IsLoadedFromMap = true;
                            }
                            else if (item.IsGroundTile)
                            {
                                groundItem = item;
                            }
                            else
                            {
                                tile = CreateTile(groundItem, item, pX, pY, pZ);
                                tile.InternalAddThing(item);
                                item.StartDecaying();
                                item.IsLoadedFromMap = true;
                            }
                        }
                        break;

                    default:
                        Logger.Log(LogLevels.Warning, "Unknown tile attribute on; X:" + pX + ", Y:" + pY + ", Z:" + pZ + "!");
                        return(false);
                    }
                }

                NodeStruct nodeItem = fileLoader.GetChildNode(nodeTile, out type);
                while (nodeItem != null)
                {
                    if (type != (byte)OtbmNodeTypes.Item)
                    {
                        return(false);
                    }

                    MemoryStream stream;
                    if (!fileLoader.GetProps(nodeItem, out stream))
                    {
                        return(false);
                    }

                    Item item = Item.CreateItem(stream);
                    if (item == null)
                    {
                        return(false);
                    }

                    //TODO: UNSERIALIZE

                    if (isHouseTile && item.IsMovable)
                    {
                        Logger.Log(LogLevels.Warning, "Moveable item found in house: " + house.HouseId);
                        item.DecrementReferenceCounter();
                    }
                    else
                    {
                        if (item.Count <= 0)
                        {
                            item.Count = 1;
                        }

                        if (tile != null)
                        {
                            tile.InternalAddThing(item);
                            item.StartDecaying();
                            item.IsLoadedFromMap = true;
                        }
                        else if (item.IsGroundTile)
                        {
                            groundItem = item;
                        }
                        else
                        {
                            tile = CreateTile(groundItem, item, pX, pY, pZ);
                            tile.InternalAddThing(item);
                            item.StartDecaying();
                            item.IsLoadedFromMap = true;
                        }
                    }

                    nodeItem = nodeItem.Next;
                }

                if (tile == null)
                {
                    tile = CreateTile(groundItem, null, pX, pY, pZ);
                }

                tile.SetFlag(tileFlags);
                SetTile(tile);

                nodeTile = fileLoader.GetNextNode(nodeTile, out type);
            }

            return(true);
        }