Exemplo n.º 1
0
 /// <summary>
 /// Construct a new instance of the AvatarAppearanceEventArgs class
 /// </summary>
 /// <param name="sim">The simulator request was from</param>
 /// <param name="avatarID">The ID of the agent</param>
 /// <param name="isTrial">true of the agent is a trial account</param>
 /// <param name="defaultTexture">The default agent texture</param>
 /// <param name="faceTextures">The agents appearance layer textures</param>
 /// <param name="visualParams">The <see cref="VisualParams"/> for the agent</param>
 public AvatarAppearanceEventArgs(Simulator sim, UUID avatarID, bool isTrial, Primitive.TextureEntryFace defaultTexture,
     Primitive.TextureEntryFace[] faceTextures, List<byte> visualParams,
     byte appearanceVersion, int COFVersion, AppearanceFlags appearanceFlags)
 {
     this.m_Simulator = sim;
     this.m_AvatarID = avatarID;
     this.m_IsTrial = isTrial;
     this.m_DefaultTexture = defaultTexture;
     this.m_FaceTextures = faceTextures;
     this.m_VisualParams = visualParams;
     this.m_AppearanceVersion = appearanceVersion;
     this.m_COFVersion = COFVersion;
     this.m_AppearanceFlags = appearanceFlags;
 }
        AppearanceFlags GenerateAppearanceFlags(Assets.ThingType thingType, Appearance appearance)
        {
            if (thingType.Attributes.Count == 0)
            {
                return(null);
            }

            var appearanceFlags = new AppearanceFlags();

            if (thingType.HasAttribute(AttributesUniform.Ground))
            {
                appearanceFlags.Ground = new AppearanceFlagGround()
                {
                    Speed = (ushort)thingType.Attributes[AttributesUniform.Ground]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.Writable))
            {
                appearanceFlags.Writable = new AppearanceFlagWritable()
                {
                    MaxTextLength = (ushort)thingType.Attributes[AttributesUniform.Writable]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.WritableOnce))
            {
                appearanceFlags.WritableOnce = new AppearanceFlagWritableOnce()
                {
                    MaxTextLengthOnce = (ushort)thingType.Attributes[AttributesUniform.WritableOnce]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.MinimapColor))
            {
                appearanceFlags.Automap = new AppearanceFlagAutomap()
                {
                    Color = (ushort)thingType.Attributes[AttributesUniform.MinimapColor]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.Elevation))
            {
                appearanceFlags.Height = new AppearanceFlagHeight()
                {
                    Elevation = (ushort)thingType.Attributes[AttributesUniform.Elevation]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.LensHelp))
            {
                appearanceFlags.LensHelp = new AppearanceFlagLensHelp()
                {
                    ID = (ushort)thingType.Attributes[AttributesUniform.LensHelp]
                }
            }
            ;
            if (thingType.HasAttribute(AttributesUniform.Cloth))
            {
                appearanceFlags.Clothes = new AppearanceFlagClothes()
                {
                    Slot = (ushort)thingType.Attributes[AttributesUniform.Cloth]
                }
            }
            ;

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

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

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

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

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

                appearanceFlags.Light = new AppearanceFlagLight()
                {
                    Intensity = lightInfo.intensity,
                    Color     = lightInfo.color,
                };
            }

            if (thingType.HasAttribute(AttributesUniform.Offset))
            {
                var displacement = (Assets.Vector2Int)thingType.Attributes[AttributesUniform.Offset];
                appearanceFlags.Offset = new AppearanceFlagOffset()
                {
                    X = displacement.x,
                    Y = displacement.y,
                };
            }

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

                appearanceFlags.Market = new AppearanceFlagMarket()
                {
                    Category        = (Protobuf.Shared.ItemCategory)market.category,
                    TradeAsObjectID = market.tradeAs,
                    ShowAsObjectID  = market.showAs,
                    MinimumLevel    = market.restrictLevel,
                };

                appearanceFlags.Market.RestrictToProfession.Add((Protobuf.Shared.PlayerProfession)market.restrictProfession);
                appearance.Name = market.name;
            }

            return(appearanceFlags);
        }

        /// <summary>
        /// Generates protobuf Appearance from assets ThingType
        /// </summary>
        /// <param name="thingType">thing generated from tibia.dat (old revisions)</param>
        /// <returns></returns>
        Appearance GenerateAppearance(Assets.ThingType thingType)
        {
            var appearance = new Appearance();

            appearance.ID    = thingType.ID;
            appearance.Flags = GenerateAppearanceFlags(thingType, appearance);

            foreach (var pair in thingType.FrameGroups)
            {
                var frameGroupType   = pair.Key;
                var legacyFrameGroup = pair.Value;

                var frameGroup = new FrameGroup();
                var spriteInfo = new SpriteInfo();
                frameGroup.Type       = pair.Key == 0 ? Protobuf.Shared.FrameGroupType.Idle : Protobuf.Shared.FrameGroupType.Walking;
                frameGroup.ID         = m_ReferenceFrameGroupID++;
                frameGroup.SpriteInfo = spriteInfo;

                spriteInfo.PatternWidth   = legacyFrameGroup.PatternWidth;
                spriteInfo.PatternHeight  = legacyFrameGroup.PatternHeight;
                spriteInfo.PatternDepth   = legacyFrameGroup.PatternDepth;
                spriteInfo.Layers         = legacyFrameGroup.Layers;
                spriteInfo.Phases         = legacyFrameGroup.Phases;
                spriteInfo.BoundingSquare = legacyFrameGroup.ExactSize;

                if (legacyFrameGroup.Animator != null)
                {
                    var animation = new SpriteAnimation();
                    spriteInfo.Animation        = animation;
                    animation.DefaultStartPhase = (uint)legacyFrameGroup.Animator.StartPhase;
                    animation.Synchronized      = !legacyFrameGroup.Animator.Async;
                    //animation.RandomStartPhase = false;

                    if (legacyFrameGroup.Animator.LoopCount < 0)
                    {
                        animation.LoopType = Protobuf.Shared.AnimationLoopType.PingPong;
                    }
                    else if (legacyFrameGroup.Animator.LoopCount == 0)
                    {
                        animation.LoopType = Protobuf.Shared.AnimationLoopType.Infinite;
                    }
                    else
                    {
                        animation.LoopType  = Protobuf.Shared.AnimationLoopType.Counted;
                        animation.LoopCount = (uint)legacyFrameGroup.Animator.LoopCount;
                    }

                    //animation.IsOpaque = false;

                    foreach (var m in legacyFrameGroup.Animator.FrameGroupDurations)
                    {
                        var spritePhase = new SpritePhase();
                        spritePhase.DurationMin = (uint)m.Minimum;
                        spritePhase.DurationMax = (uint)m.Maximum;

                        animation.SpritePhases.Add(spritePhase);
                    }
                }

                foreach (var spriteID in legacyFrameGroup.Sprites)
                {
                    spriteInfo.SpriteIDs.Add(spriteID);
                }

                m_FrameGroupDetails.Add(frameGroup, new FrameGroupDetail(legacyFrameGroup.Width, legacyFrameGroup.Height));
                appearance.FrameGroups.Add(frameGroup);
            }

            return(appearance);
        }

        Appearances GenerateAppearances()
        {
            try {
                var rawContentDat = File.ReadAllBytes(Path.Combine(m_ClientVersion.ToString(), "Tibia.dat"));
                var contentData   = new Assets.ContentData(rawContentDat, m_ClientVersion);

                var appearances = new Appearances();
                for (int i = 0; i < contentData.ThingTypeDictionaries.Length; i++)
                {
                    var dict = contentData.ThingTypeDictionaries[i];
                    foreach (var pair in dict)
                    {
                        var appearance = GenerateAppearance(pair.Value);
                        switch (i)
                        {
                        case 0: appearances.Objects.Add(appearance); break;

                        case 1: appearances.Outfits.Add(appearance); break;

                        case 2: appearances.Effects.Add(appearance); break;

                        case 3: appearances.Missles.Add(appearance); break;
                        }
                    }
                }

                m_JsonTokens.Add(new AppearancesToken()
                {
                    file = "appearances.dat"
                });
                return(appearances);
            } catch (Exception e) {
                Console.WriteLine(e.Message + '\n' + e.StackTrace);
                Environment.Exit(0);
            }

            return(null);
        }