Example #1
0
        public void Parse()
        {
            DatSignature = m_BinaryReader.GetU32();
            Console.WriteLine("Dat Signature: 0x" + DatSignature.ToString("X"));
            ContentRevision = (ushort)DatSignature;

            int[] counts = new int[(int)ThingCategory.LastCategory];
            for (int category = 0; category < (int)ThingCategory.LastCategory; category++)
            {
                int count = m_BinaryReader.GetU16() + 1;
                counts[category] = count;
            }

            for (int category = 0; category < (int)ThingCategory.LastCategory; category++)
            {
                ushort firstId = 1;
                if (category == (int)ThingCategory.Item)
                {
                    firstId = 100;
                }

                ThingTypeDictionaries[category] = new ThingTypesDict();
                for (ushort id = firstId; id < counts[category]; id++)
                {
                    ThingType thingType = new ThingType()
                    {
                        ID       = id,
                        Category = (ThingCategory)category,
                    };

                    thingType.Unserialize(m_BinaryReader, m_ClientVersion);
                    ThingTypeDictionaries[category][id] = thingType;
                }
            }
        }
Example #2
0
        public void Parse()
        {
            DatSignature    = m_BinaryReader.GetU32();
            ContentRevision = (ushort)DatSignature;

            int[] counts = new int[(int)ThingCategory.LastCategory];
            for (int category = 0; category < (int)ThingCategory.LastCategory; category++)
            {
                int count = m_BinaryReader.GetU16() + 1;
                counts[category] = count;
            }

            for (int category = 0; category < (int)ThingCategory.LastCategory; category++)
            {
                ushort firstId = 1;
                if (category == (int)ThingCategory.Item)
                {
                    firstId = 100;
                }

                ThingTypes[category] = new ThingTypesDict();
                for (ushort id = firstId; id < counts[category]; id++)
                {
                    ThingType thingType = ThingType.Serialize(id, (ThingCategory)category, ref m_BinaryReader);
                    ThingTypes[category][id] = thingType;
                }
            }
        }
Example #3
0
        public void Serialize(ThingType thingType, Net.OutputMessage binaryWriter, int fromVersion, int newVersion, sbyte startPhase, byte phasesLimit)
        {
            binaryWriter.AddU8(Width);
            binaryWriter.AddU8(Height);
            if (Width > 1 || Height > 1)
            {
                binaryWriter.AddU8(ExactSize);
            }

            binaryWriter.AddU8(Layers);
            binaryWriter.AddU8(PatternWidth);
            binaryWriter.AddU8(PatternHeight);
            if (newVersion >= 755)
            {
                binaryWriter.AddU8(PatternDepth);
            }

            binaryWriter.AddU8(phasesLimit);

            if (fromVersion < 1050)
            {
                if (phasesLimit > 1 && newVersion >= 1050)
                {
                    FrameGroupAnimator.SerializeLegacy(thingType, binaryWriter, startPhase, phasesLimit);
                }
            }
            else
            {
                if (phasesLimit > 1 && newVersion >= 1050)
                {
                    Animator.Serialize(binaryWriter, startPhase, phasesLimit);
                }
            }

            int spritesPerPhase = Width * Height * Layers * PatternWidth * PatternHeight * PatternDepth;
            int totalSprites    = phasesLimit * spritesPerPhase;
            int offset          = startPhase * spritesPerPhase;

            for (int j = 0; j < totalSprites; j++)
            {
                uint spriteId = Sprites[offset + j];
                if (newVersion >= 960)
                {
                    binaryWriter.AddU32(spriteId);
                }
                else
                {
                    binaryWriter.AddU16((ushort)spriteId);
                }
            }
        }
Example #4
0
        public static void SerializeLegacy(ThingType thingType, Net.OutputMessage binaryWriter, int startPhase, int phasesLimit)
        {
            binaryWriter.AddU8(1);
            binaryWriter.AddS32(thingType.HasAttribute(AttributesUniform.AnimateAlways) || thingType.Category == ThingCategory.Item ? 0 : 1);
            binaryWriter.AddU8(0);

            int duration;

            if (thingType.Category == ThingCategory.Effect)
            {
                duration = 75;
            }
            else
            {
                duration = phasesLimit > 0 ? 1000 / phasesLimit : 40;
            }

            for (int i = 0; i < phasesLimit; i++)
            {
                binaryWriter.AddS32(duration); // force legacy animation
                binaryWriter.AddS32(duration);
            }
        }
Example #5
0
        public static ThingType Serialize(ushort id, ThingCategory category, ref Net.InputMessage binaryReader)
        {
            ThingType type = new ThingType()
            {
                ID       = id,
                Category = category
            };

            int  count = 0, attr = -1;
            bool done = false;

            for (int i = 0; i < DatAttributes.LastAttr; ++i)
            {
                count++;
                attr = binaryReader.GetU8();
                if (attr == DatAttributes.LastAttr)
                {
                    done = true;
                    break;
                }

                switch (attr)
                {
                case DatAttributes.Ground:
                case DatAttributes.Writable:
                case DatAttributes.WritableOnce:
                case DatAttributes.MinimapColor:
                case DatAttributes.LensHelp:
                case DatAttributes.Cloth:
                case DatAttributes.DefaultAction:
                    type.Attributes[(byte)attr] = binaryReader.GetU16();
                    break;

                case DatAttributes.GroundBorder:
                case DatAttributes.OnBottom:
                case DatAttributes.OnTop:
                case DatAttributes.Container:
                case DatAttributes.Stackable:
                case DatAttributes.ForceUse:
                case DatAttributes.MultiUse:
                case DatAttributes.FluidContainer:
                case DatAttributes.Splash:
                case DatAttributes.NotWalkable:
                case DatAttributes.NotMoveable:
                case DatAttributes.BlockProjectile:
                case DatAttributes.NotPathable:
                case DatAttributes.NoMoveAnimation:
                case DatAttributes.Pickupable:
                case DatAttributes.Hangable:
                case DatAttributes.HookSouth:
                case DatAttributes.HookEast:
                case DatAttributes.Rotateable:
                case DatAttributes.DontHide:
                case DatAttributes.Translucent:
                case DatAttributes.LyingCorpse:
                case DatAttributes.AnimateAlways:
                case DatAttributes.FullGround:
                case DatAttributes.Look:
                case DatAttributes.Wrapable:
                case DatAttributes.Unwrapable:
                case DatAttributes.TopEffect:
                case DatAttributes.Usable:
                    type.Attributes[(byte)attr] = true;
                    break;

                case DatAttributes.Light:
                    type.Attributes[(byte)attr] = new LightInfo()
                    {
                        intensity = binaryReader.GetU16(),
                        color     = binaryReader.GetU16()
                    };
                    break;

                case DatAttributes.Displacement:
                    type.Attributes[(byte)attr] = new Vector2Int {
                        x = binaryReader.GetU16(),
                        y = binaryReader.GetU16()
                    };
                    break;

                case DatAttributes.Elevation:
                    type.Elevation = binaryReader.GetU16();
                    type.Attributes[(byte)attr] = type.Elevation;
                    break;

                case DatAttributes.Market:
                    type.Attributes[(byte)attr] = new MarketData()
                    {
                        category         = binaryReader.GetU16(),
                        tradeAs          = binaryReader.GetU16(),
                        showAs           = binaryReader.GetU16(),
                        name             = binaryReader.GetString(),
                        restrictVocation = binaryReader.GetU16(),
                        requiredLevel    = binaryReader.GetU16(),
                    };
                    break;

                default:
                    throw new System.Exception("Unhandled DatAttribute [" + attr + "].");
                }
            }

            if (!done)
            {
                throw new System.Exception("Couldn't parse thing [category: " + category + ", ID: " + id + "].");
            }

            byte groupCount = (category == ThingCategory.Creature) ? binaryReader.GetU8() : (byte)1U;

            for (int i = 0; i < groupCount; i++)
            {
                FrameGroupType groupType = FrameGroupType.Default;
                if (category == ThingCategory.Creature)
                {
                    groupType = (FrameGroupType)binaryReader.GetU8();
                }

                type.FrameGroups[groupType] = FrameGroup.Serialize(category, ref binaryReader);
            }

            return(type);
        }
Example #6
0
        /// <summary>
        /// Generates protobuf appearance from the legacy thingtype
        /// </summary>
        /// <param name="thingType">thing generated from tibia.dat (old revisions)</param>
        /// <returns></returns>
        static Appearance GenerateAppearance(Core.Sprites.ThingType thingType)
        {
            Appearance appearance = new Appearance()
            {
                Id = thingType.ID,
            };

            if (thingType.Attributes.Count > 0)
            {
                appearance.Flags = new AppearanceFlags();
            }

            // Flags
            if (thingType.HasAttribute(AttributesUniform.Ground))
            {
                appearance.Flags.Ground = new Ground()
                {
                    Speed = (ushort)thingType.Attributes[AttributesUniform.Ground]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.Writable))
            {
                appearance.Flags.Writable = new Writable()
                {
                    Length = (ushort)thingType.Attributes[AttributesUniform.Writable]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.WritableOnce))
            {
                appearance.Flags.WritableOnce = new Writable()
                {
                    Length = (ushort)thingType.Attributes[AttributesUniform.WritableOnce]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.MinimapColor))
            {
                appearance.Flags.Minimap = new MiniMap()
                {
                    Color = (ushort)thingType.Attributes[AttributesUniform.MinimapColor]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.Elevation))
            {
                appearance.Flags.Elevation = new Elevation()
                {
                    Elevation_ = (ushort)thingType.Attributes[AttributesUniform.Elevation]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.LensHelp))
            {
                appearance.Flags.LensHelp = new LensHelp()
                {
                    Id = (ushort)thingType.Attributes[AttributesUniform.LensHelp]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.Cloth))
            {
                appearance.Flags.Cloth = new Clothes()
                {
                    Slot = (ushort)thingType.Attributes[AttributesUniform.Cloth]
                }
            }
            ;

            // default action
            if (thingType.HasAttribute(AttributesUniform.DefaultAction))
            {
                var defaultAction         = new DefaultAction();
                var oldDefaultActionValue = (ushort)thingType.Attributes[AttributesUniform.DefaultAction];
                if (oldDefaultActionValue > 4)
                {
                    Console.WriteLine("Invalid default action: " + oldDefaultActionValue + " for item id: " + thingType.ID);
                }
                appearance.Flags.DefaultAction = new DefaultAction()
                {
                    Action = (PlayerAction)oldDefaultActionValue
                };
            }

            if (thingType.HasAttribute(AttributesUniform.GroundBorder))
            {
                appearance.Flags.GroundBorder = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Bottom))
            {
                appearance.Flags.Bottom = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Top))
            {
                appearance.Flags.Top = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Container))
            {
                appearance.Flags.Container = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Stackable))
            {
                appearance.Flags.Stackable = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Use))
            {
                appearance.Flags.Use = true;
            }
            if (thingType.HasAttribute(AttributesUniform.ForceUse))
            {
                appearance.Flags.ForceUse = true;
            }
            if (thingType.HasAttribute(AttributesUniform.MultiUse))
            {
                appearance.Flags.MultiUse = true;
            }
            if (thingType.HasAttribute(AttributesUniform.FluidContainer))
            {
                appearance.Flags.FluidContainer = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Splash))
            {
                appearance.Flags.Splash = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Unpassable))
            {
                appearance.Flags.Unpassable = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Unmoveable))
            {
                appearance.Flags.Unmoveable = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Unsight))
            {
                appearance.Flags.Unsight = true;
            }
            if (thingType.HasAttribute(AttributesUniform.BlockPath))
            {
                appearance.Flags.BlockPath = true;
            }
            if (thingType.HasAttribute(AttributesUniform.NoMoveAnimation))
            {
                appearance.Flags.NoMoveAnimation = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Pickupable))
            {
                appearance.Flags.Pickupable = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Hangable))
            {
                appearance.Flags.Hangable = true;
            }

            // can have only one hook //
            if (thingType.HasAttribute(AttributesUniform.HookSouth))
            {
                appearance.Flags.Hook = new Hook()
                {
                    Type = HookType.South
                }
            }
            ;
            else if (thingType.HasAttribute(AttributesUniform.HookEast))
            {
                appearance.Flags.Hook = new Hook()
                {
                    Type = HookType.East
                }
            }
            ;

            if (thingType.HasAttribute(AttributesUniform.Rotateable))
            {
                appearance.Flags.Rotateable = true;
            }
            if (thingType.HasAttribute(AttributesUniform.DontHide))
            {
                appearance.Flags.DontHide = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Translucent))
            {
                appearance.Flags.Translucent = true;
            }
            if (thingType.HasAttribute(AttributesUniform.LyingCorpse))
            {
                appearance.Flags.LyingCorpse = true;
            }
            if (thingType.HasAttribute(AttributesUniform.AnimateAlways))
            {
                appearance.Flags.AnimateAlways = true;
            }
            if (thingType.HasAttribute(AttributesUniform.FullGround))
            {
                appearance.Flags.FullGround = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Look))
            {
                appearance.Flags.Look = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Wrapable))
            {
                appearance.Flags.Wrapable = true;
            }
            if (thingType.HasAttribute(AttributesUniform.Unwrapable))
            {
                appearance.Flags.GroundBorder = true;
            }
            if (thingType.HasAttribute(AttributesUniform.TopEffect))
            {
                appearance.Flags.TopEffect = true;
            }

            if (thingType.HasAttribute(AttributesUniform.Light))
            {
                var lightInfo = (Core.Sprites.LightInfo)thingType.Attributes[AttributesUniform.Light];

                appearance.Flags.Light = new LightInfo()
                {
                    Intensity = lightInfo.intensity,
                    Color     = lightInfo.color,
                };
            }

            if (thingType.HasAttribute(AttributesUniform.Offset))
            {
                var displacement = (Core.Sprites.Vector2Int)thingType.Attributes[AttributesUniform.Offset];
                appearance.Flags.Displacement = new Displacement()
                {
                    X = displacement.x,
                    Y = displacement.y,
                };
            }

            if (thingType.HasAttribute(AttributesUniform.Market))
            {
                var Market = (Core.Sprites.MarketData)thingType.Attributes[AttributesUniform.Market];

                appearance.Flags.Market = new MarketInfo()
                {
                    Category         = Market.category,
                    TradeAs          = Market.tradeAs,
                    ShowAs           = Market.showAs,
                    Name             = Market.name,
                    RestrictVocation = Market.restrictProfession,
                    RequiredLevel    = Market.restrictLevel,
                };
            }

            foreach (var f in thingType.FrameGroups)
            {
                FrameGroup frameGroup = new FrameGroup();

                frameGroup.Type          = f.Key == 0 ? FrameGroupType.Idle : FrameGroupType.Walking;
                frameGroup.Height        = f.Value.Height;
                frameGroup.Width         = f.Value.Width;
                frameGroup.ExactSize     = f.Value.ExactSize;
                frameGroup.Layers        = f.Value.Layers;
                frameGroup.PatternWidth  = f.Value.PatternWidth;
                frameGroup.PatternHeight = f.Value.PatternHeight;
                frameGroup.PatternDepth  = f.Value.PatternDepth;
                frameGroup.Phases        = f.Value.Phases;

                if (f.Value.Animator != null)
                {
                    frameGroup.FrameAnimation            = new FrameAnimation();
                    frameGroup.FrameAnimation.Async      = f.Value.Animator.Async;
                    frameGroup.FrameAnimation.LoopCount  = f.Value.Animator.LoopCount;
                    frameGroup.FrameAnimation.StartPhase = f.Value.Animator.StartPhase;

                    foreach (var m in f.Value.Animator.FrameGroupDurations)
                    {
                        FrameGroupDuration duration = new FrameGroupDuration();
                        duration.Min = (uint)m.Minimum;
                        duration.Max = (uint)m.Maximum;

                        frameGroup.FrameAnimation.FrameGroupDurations.Add(duration);
                    }
                }

                foreach (var s in f.Value.Sprites)
                {
                    frameGroup.Sprites.Add(s);
                }

                appearance.FrameGroups.Add(frameGroup);
            }

            return(appearance);
        }