Example #1
0
 public ItemData(ItemPrefix prefix, ItemType type, int stackSize = 1)
 {
     if (type != ItemType.None && stackSize >= 1) {
     this.Prefix = prefix;
     this.Type = type;
     this.StackSize = stackSize;
       } else {
     // become ItemData.None
     this.Prefix = ItemPrefix.None;
     this.Type = ItemType.None;
     this.StackSize = 0;
       }
 }
Example #2
0
        private static void LoadObjectDbXml(string file)
        {
            var xmlSettings = XElement.Load(file);

            // Load Colors
            foreach (var xElement in xmlSettings.Elements("GlobalColors").Elements("GlobalColor"))
            {
                string    name  = (string)xElement.Attribute("Name");
                XNA.Color color = XnaColorFromString((string)xElement.Attribute("Color"));
                GlobalColors.Add(name, color);
            }

            foreach (var xElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                var curTile = new TileProperty();

                // Read XML attributes
                curTile.Color        = ColorFromString((string)xElement.Attribute("Color"));
                curTile.Name         = (string)xElement.Attribute("Name");
                curTile.Id           = (int?)xElement.Attribute("Id") ?? 0;
                curTile.IsFramed     = (bool?)xElement.Attribute("Framed") ?? false;
                curTile.IsSolid      = (bool?)xElement.Attribute("Solid") ?? false;
                curTile.IsSolidTop   = (bool?)xElement.Attribute("SolidTop") ?? false;
                curTile.IsLight      = (bool?)xElement.Attribute("Light") ?? false;
                curTile.FrameSize    = StringToVector2Short((string)xElement.Attribute("Size"), 1, 1);
                curTile.Placement    = InLineEnumTryParse <FramePlacement>((string)xElement.Attribute("Placement"));
                curTile.TextureGrid  = StringToVector2Short((string)xElement.Attribute("TextureGrid"), 16, 16);
                curTile.IsGrass      = "Grass".Equals((string)xElement.Attribute("Special"));    /* Heathtech */
                curTile.IsPlatform   = "Platform".Equals((string)xElement.Attribute("Special")); /* Heathtech */
                curTile.IsCactus     = "Cactus".Equals((string)xElement.Attribute("Special"));   /* Heathtech */
                curTile.IsStone      = (bool?)xElement.Attribute("Stone") ?? false;              /* Heathtech */
                curTile.CanBlend     = (bool?)xElement.Attribute("Blends") ?? false;             /* Heathtech */
                curTile.MergeWith    = (int?)xElement.Attribute("MergeWith") ?? null;            /* Heathtech */
                curTile.HasFrameName = curTile.IsFramed && ((bool?)xElement.Attribute("UseFrameName") ?? false);
                string frameNamePostfix = (string)xElement.Attribute("FrameNamePostfix") ?? null;

                foreach (var elementFrame in xElement.Elements("Frames").Elements("Frame"))
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = (string)elementFrame.Attribute("Name");
                    curFrame.Variety = (string)elementFrame.Attribute("Variety");
                    curFrame.UV      = StringToVector2Short((string)elementFrame.Attribute("UV"), 0, 0);
                    curFrame.Anchor  = InLineEnumTryParse <FrameAnchor>((string)elementFrame.Attribute("Anchor"));

                    // Assign a default name if none existed
                    if (string.IsNullOrWhiteSpace(curFrame.Name))
                    {
                        curFrame.Name = curTile.Name;
                    }

                    curTile.Frames.Add(curFrame);
                    Sprites.Add(new Sprite
                    {
                        Anchor           = curFrame.Anchor,
                        IsPreviewTexture = false,
                        Name             = curFrame.Name + ", " + curFrame.Variety,
                        Origin           = curFrame.UV,
                        Size             = curTile.FrameSize,
                        Tile             = (ushort)curTile.Id,     /* SBlogic */
                        TileName         = curTile.Name
                    });
                    if (curTile.HasFrameName)
                    {
                        string frameName = curFrame.Name;
                        if (frameNamePostfix != null)
                        {
                            frameName += " (" + frameNamePostfix + ")";
                        }
                        if (curFrame.Variety != null)
                        {
                            frameName += ", " + curFrame.Variety;
                        }

                        //  TODO:  There must be a more efficient way than to store each frame...
                        for (int x = 0, mx = curTile.FrameSize.X; x < mx; x++)
                        {
                            for (int y = 0, my = curTile.FrameSize.Y; y < my; y++)
                            {
                                string frameNameKey = GetFrameNameKey(curTile.Id, (short)(curFrame.UV.X + (x * 18)), (short)(curFrame.UV.Y + (y * 18)));
                                if (!FrameNames.ContainsKey(frameNameKey))
                                {
                                    FrameNames.Add(frameNameKey, frameName);
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine(curFrame.Name + " collided with " + frameNameKey);
                                }
                            }
                        }
                    }
                }
                if (curTile.Frames.Count == 0 && curTile.IsFramed)
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = curTile.Name;
                    curFrame.Variety = string.Empty;
                    curFrame.UV      = new Vector2Short(0, 0);
                    //curFrame.Anchor = InLineEnumTryParse<FrameAnchor>((string)xElement.Attribute("Anchor"));

                    // Assign a default name if none existed
                    if (string.IsNullOrWhiteSpace(curFrame.Name))
                    {
                        curFrame.Name = curTile.Name;
                    }

                    curTile.Frames.Add(curFrame);
                    Sprites.Add(new Sprite
                    {
                        Anchor           = curFrame.Anchor,
                        IsPreviewTexture = false,
                        Name             = curFrame.Name + ", " + curFrame.Variety,
                        Origin           = curFrame.UV,
                        Size             = curTile.FrameSize,
                        Tile             = (ushort)curTile.Id,
                        TileName         = curTile.Name
                    });
                }
                TileProperties.Add(curTile);
                if (!curTile.IsFramed)
                {
                    TileBricks.Add(curTile);
                }
            }
            for (int i = TileProperties.Count; i < 255; i++)
            {
                TileProperties.Add(new TileProperty(i, "UNKNOWN", Color.FromArgb(255, 255, 0, 255), true));
            }

            foreach (var xElement in xmlSettings.Elements("Walls").Elements("Wall"))
            {
                var curWall = new WallProperty();
                curWall.Color   = ColorFromString((string)xElement.Attribute("Color"));
                curWall.Name    = (string)xElement.Attribute("Name");
                curWall.Id      = (int?)xElement.Attribute("Id") ?? -1;
                curWall.IsHouse = (bool?)xElement.Attribute("IsHouse") ?? false;
                WallProperties.Add(curWall);
            }

            foreach (var xElement in xmlSettings.Elements("Items").Elements("Item"))
            {
                var curItem = new ItemProperty();
                curItem.Id   = (int?)xElement.Attribute("Id") ?? -1;
                curItem.Name = (string)xElement.Attribute("Name");
                ItemProperties.Add(curItem);
                _itemLookup.Add(curItem.Id, curItem);
                int tally = (int?)xElement.Attribute("Tally") ?? 0;
                if (tally > 0)
                {
                    _tallynames.Add(tally, curItem.Name);
                }
            }

            foreach (var xElement in xmlSettings.Elements("Paints").Elements("Paint"))
            {
                var curPaint = new PaintProperty();
                curPaint.Id    = (int?)xElement.Attribute("Id") ?? -1;
                curPaint.Name  = (string)xElement.Attribute("Name");
                curPaint.Color = ColorFromString((string)xElement.Attribute("Color"));
                PaintProperties.Add(curPaint);
            }

            int chestId = 0;

            foreach (var tileElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                string tileName = (string)tileElement.Attribute("Name");
                if (tileName == "Chest" || tileName == "Dresser")
                {
                    ushort type = (ushort)((int?)tileElement.Attribute("Id") ?? 21);
                    foreach (var xElement in tileElement.Elements("Frames").Elements("Frame"))
                    {
                        var curItem = new ChestProperty();
                        curItem.Name = (string)xElement.Attribute("Name");
                        string variety = (string)xElement.Attribute("Variety");
                        if (variety != null)
                        {
                            if (tileName == "Dresser")
                            {
                                curItem.Name = variety + " " + "Dresser";
                            }
                            else
                            {
                                curItem.Name = curItem.Name + " " + variety;
                            }
                        }
                        curItem.ChestId  = chestId++;
                        curItem.UV       = StringToVector2Short((string)xElement.Attribute("UV"), 0, 0);
                        curItem.TileType = type;
                        ChestProperties.Add(curItem);
                    }
                }
            }

            int signId = 0;

            foreach (var tileElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                string tileName = (string)tileElement.Attribute("Name");
                if (tileName == "Sign" || tileName == "Grave Marker")
                {
                    ushort type = (ushort)((int?)tileElement.Attribute("Id") ?? 55);
                    foreach (var xElement in tileElement.Elements("Frames").Elements("Frame"))
                    {
                        var    curItem = new SignProperty();
                        string variety = (string)xElement.Attribute("Variety");
                        if (variety != null)
                        {
                            if (tileName == "Sign")
                            {
                                curItem.Name = "Sign " + variety;
                            }
                            else
                            {
                                curItem.Name = variety;
                            }
                        }
                        curItem.SignId   = signId++;
                        curItem.UV       = StringToVector2Short((string)xElement.Attribute("UV"), 0, 0);
                        curItem.TileType = type;
                        SignProperties.Add(curItem);
                    }
                }
            }

            foreach (var xElement in xmlSettings.Elements("Npcs").Elements("Npc"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                NpcIds.Add(name, id);
                NpcNames.Add(id, name);
                int frames = (int?)xElement.Attribute("Frames") ?? 16;
                NpcFrames.Add(id, frames);
            }

            foreach (var xElement in xmlSettings.Elements("ItemPrefix").Elements("Prefix"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                ItemPrefix.Add((byte)id, name);
            }

            foreach (var xElement in xmlSettings.Elements("ShortCutKeys").Elements("Shortcut"))
            {
                var key  = InLineEnumTryParse <Key>((string)xElement.Attribute("Key"));
                var tool = (string)xElement.Attribute("Tool");
                ShortcutKeys.Add(key, tool);
            }

            XElement appSettings   = xmlSettings.Element("App");
            int      appWidth      = (int?)appSettings.Attribute("Width") ?? 800;
            int      appHeight     = (int?)appSettings.Attribute("Height") ?? 600;
            int      clipboardSize = (int)XNA.MathHelper.Clamp((int?)appSettings.Attribute("ClipboardRenderSize") ?? 512, 64, 4096);

            _appSize = new Vector2(appWidth, appHeight);
            ClipboardBuffer.ClipboardRenderSize = clipboardSize;

            ToolDefaultData.LoadSettings(xmlSettings.Elements("Tools"));

            AltC        = (string)xmlSettings.Element("AltC");
            SteamUserId = (int?)xmlSettings.Element("SteamUserId") ?? null;
        }
        public bool IsUniversalPrefix(ItemPrefix prefix)
        {
            switch (prefix) {
            case ItemPrefix.Keen:
            case ItemPrefix.Superior:
            case ItemPrefix.Forceful:
            case ItemPrefix.Broken:
            case ItemPrefix.Damaged:
            case ItemPrefix.Shoddy:
            case ItemPrefix.Hurtful:
            case ItemPrefix.Strong:
            case ItemPrefix.Unpleasant:
            case ItemPrefix.Weak:
            case ItemPrefix.Ruthless:
            case ItemPrefix.Godly:
            case ItemPrefix.Demonic:
            case ItemPrefix.Zealous:
              return true;
              }

              return false;
        }
        public bool IsCommonPrefix(ItemPrefix prefix)
        {
            switch (prefix) {
            case ItemPrefix.Quick:
            case ItemPrefix.Quick2:
            case ItemPrefix.Deadly:
            case ItemPrefix.Deadly2:
            case ItemPrefix.Agile:
            case ItemPrefix.Nimble:
            case ItemPrefix.Murderous:
            case ItemPrefix.Slow:
            case ItemPrefix.Sluggish:
            case ItemPrefix.Lazy:
            case ItemPrefix.Annoying:
            case ItemPrefix.Nasty:
              return true;
              }

              return false;
        }
 public string GetItemPrefixName(ItemPrefix prefix)
 {
     return Lang.prefix[(int)prefix];
 }
        private static void LoadObjectDbXml(string file)
        {
            var xmlSettings = XElement.Load(file);

            // Load Colors
            foreach (var xElement in xmlSettings.Elements("GlobalColors").Elements("GlobalColor"))
            {
                string    name  = (string)xElement.Attribute("Name");
                XNA.Color color = XnaColorFromString((string)xElement.Attribute("Color"));
                GlobalColors.Add(name, color);
            }

            foreach (var xElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                var curTile = new TileProperty();

                // Read XML attributes
                curTile.Color      = ColorFromString((string)xElement.Attribute("Color"));
                curTile.Name       = (string)xElement.Attribute("Name");
                curTile.Id         = (int?)xElement.Attribute("Id") ?? 0;
                curTile.IsFramed   = (bool?)xElement.Attribute("Framed") ?? false;
                curTile.IsSolid    = (bool?)xElement.Attribute("Solid") ?? false;
                curTile.IsSolidTop = (bool?)xElement.Attribute("SolidTop") ?? false;
                curTile.IsLight    = (bool?)xElement.Attribute("Light") ?? false;
                curTile.IsAnimated = (bool?)xElement.Attribute("IsAnimated") ?? false;
                curTile.FrameSize  = StringToVector2ShortArray((string)xElement.Attribute("Size"), 1, 1);
                // curTile.FrameSize = StringToVector2Short((string)xElement.Attribute("Size"), 1, 1);
                curTile.Placement   = InLineEnumTryParse <FramePlacement>((string)xElement.Attribute("Placement"));
                curTile.TextureGrid = StringToVector2Short((string)xElement.Attribute("TextureGrid"), 16, 16);
                curTile.FrameGap    = StringToVector2Short((string)xElement.Attribute("FrameGap"), 0, 0);
                curTile.IsGrass     = "Grass".Equals((string)xElement.Attribute("Special"));    /* Heathtech */
                curTile.IsPlatform  = "Platform".Equals((string)xElement.Attribute("Special")); /* Heathtech */
                curTile.IsCactus    = "Cactus".Equals((string)xElement.Attribute("Special"));   /* Heathtech */
                curTile.IsStone     = (bool?)xElement.Attribute("Stone") ?? false;              /* Heathtech */
                curTile.CanBlend    = (bool?)xElement.Attribute("Blends") ?? false;             /* Heathtech */
                curTile.MergeWith   = (int?)xElement.Attribute("MergeWith") ?? null;            /* Heathtech */
                string frameNamePostfix = (string)xElement.Attribute("FrameNamePostfix") ?? null;

                foreach (var elementFrame in xElement.Elements("Frames").Elements("Frame"))
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = (string)elementFrame.Attribute("Name");
                    curFrame.Variety = (string)elementFrame.Attribute("Variety");
                    curFrame.UV      = StringToVector2Short((string)elementFrame.Attribute("UV"), 0, 0);
                    curFrame.Anchor  = InLineEnumTryParse <FrameAnchor>((string)elementFrame.Attribute("Anchor"));
                    var frameSize = StringToVector2Short((string)elementFrame.Attribute("FrameSize"), curTile.FrameSize[0].X, curTile.FrameSize[0].Y);

                    // Assign a default name if none existed
                    if (string.IsNullOrWhiteSpace(curFrame.Name))
                    {
                        curFrame.Name = curTile.Name;
                    }

                    curTile.Frames.Add(curFrame);
                    string spriteName = null;
                    if (curFrame.Name == curTile.Name)
                    {
                        if (!string.IsNullOrWhiteSpace(curFrame.Variety))
                        {
                            spriteName += curFrame.Variety;
                        }
                    }
                    else
                    {
                        spriteName += curFrame.Name;
                        if (!string.IsNullOrWhiteSpace(curFrame.Variety))
                        {
                            spriteName += " - " + curFrame.Variety;
                        }
                    }
                    Sprites.Add(new Sprite
                    {
                        Anchor           = curFrame.Anchor,
                        IsPreviewTexture = false,
                        Name             = spriteName,
                        Origin           = curFrame.UV,
                        Size             = frameSize,
                        Tile             = (ushort)curTile.Id, /* SBlogic */
                        TileName         = curTile.Name
                    });
                }
                if (curTile.Frames.Count == 0 && curTile.IsFramed)
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = curTile.Name;
                    curFrame.Variety = string.Empty;
                    curFrame.UV      = new Vector2Short(0, 0);
                    //curFrame.Anchor = InLineEnumTryParse<FrameAnchor>((string)xElement.Attribute("Anchor"));

                    curTile.Frames.Add(curFrame);
                    Sprites.Add(new Sprite
                    {
                        Anchor           = curFrame.Anchor,
                        IsPreviewTexture = false,
                        Name             = null,
                        Origin           = curFrame.UV,
                        Size             = curTile.FrameSize[0],
                        Tile             = (ushort)curTile.Id,
                        TileName         = curTile.Name
                    });
                }
                TileProperties.Add(curTile);
                if (!curTile.IsFramed)
                {
                    TileBricks.Add(curTile);
                }
            }
            for (int i = TileProperties.Count; i < 255; i++)
            {
                TileProperties.Add(new TileProperty(i, "UNKNOWN", Color.FromArgb(255, 255, 0, 255), true));
            }

            foreach (var xElement in xmlSettings.Elements("Walls").Elements("Wall"))
            {
                var curWall = new WallProperty();
                curWall.Color = ColorFromString((string)xElement.Attribute("Color"));
                curWall.Name  = (string)xElement.Attribute("Name");
                curWall.Id    = (int?)xElement.Attribute("Id") ?? -1;
                WallProperties.Add(curWall);
            }

            foreach (var xElement in xmlSettings.Elements("Items").Elements("Item"))
            {
                var curItem = new ItemProperty();
                curItem.Id    = (int?)xElement.Attribute("Id") ?? -1;
                curItem.Name  = (string)xElement.Attribute("Name");
                curItem.Scale = (float?)xElement.Attribute("Scale") ?? 1f;

                ItemProperties.Add(curItem);
                _itemLookup.Add(curItem.Id, curItem);
                int tally = (int?)xElement.Attribute("Tally") ?? 0;
                if (tally > 0)
                {
                    _tallynames.Add(tally, curItem.Name);
                }
                int head = (int?)xElement.Attribute("Head") ?? -1;
                if (head >= 0)
                {
                    _armorHeadNames.Add(head, curItem.Name);
                }
                int body = (int?)xElement.Attribute("Body") ?? -1;
                if (body >= 0)
                {
                    _armorBodyNames.Add(body, curItem.Name);
                }
                int legs = (int?)xElement.Attribute("Legs") ?? -1;
                if (legs >= 0)
                {
                    _armorLegsNames.Add(legs, curItem.Name);
                }
                bool rack = (bool?)xElement.Attribute("Rack") ?? false;
                if (rack)
                {
                    _rackable.Add(curItem.Id, curItem.Name);
                }

                bool food = (bool?)xElement.Attribute("IsFood") ?? false;
                if (food)
                {
                    _foodNames.Add(curItem.Id, curItem.Name);
                    curItem.IsFood = true;
                }

                bool acc = (bool?)xElement.Attribute("Accessory") ?? false;
                if (acc)
                {
                    _accessoryNames.Add(curItem.Id, curItem.Name);
                }

                if (curItem.Name.Contains("Dye"))
                {
                    _dyeNames.Add(curItem.Id, curItem.Name);
                }
            }

            foreach (var xElement in xmlSettings.Elements("Paints").Elements("Paint"))
            {
                var curPaint = new PaintProperty();
                curPaint.Id    = (int?)xElement.Attribute("Id") ?? -1;
                curPaint.Name  = (string)xElement.Attribute("Name");
                curPaint.Color = ColorFromString((string)xElement.Attribute("Color"));
                PaintProperties.Add(curPaint);
            }

            int chestId = 0;

            foreach (var tileElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                string tileName = (string)tileElement.Attribute("Name");
                int    type     = (int)tileElement.Attribute("Id");
                if (Tile.IsChest(type))
                {
                    foreach (var xElement in tileElement.Elements("Frames").Elements("Frame"))
                    {
                        var curItem = new ChestProperty();
                        curItem.Name = (string)xElement.Attribute("Name");
                        string variety = (string)xElement.Attribute("Variety");
                        if (variety != null)
                        {
                            if (tileName == "Dresser")
                            {
                                curItem.Name = variety + " " + "Dresser";
                            }
                            else
                            {
                                curItem.Name = curItem.Name + " " + variety;
                            }
                        }
                        curItem.ChestId  = chestId++;
                        curItem.UV       = StringToVector2Short((string)xElement.Attribute("UV"), 0, 0);
                        curItem.TileType = (ushort)type;
                        ChestProperties.Add(curItem);
                    }
                }
            }

            int signId = 0;

            foreach (var tileElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                var    tileId   = (int?)tileElement.Attribute("Id") ?? 0;
                string tileName = (string)tileElement.Attribute("Name");
                if (Tile.IsSign(tileId))
                {
                    ushort type = (ushort)((int?)tileElement.Attribute("Id") ?? 55);
                    foreach (var xElement in tileElement.Elements("Frames").Elements("Frame"))
                    {
                        var    curItem = new SignProperty();
                        string variety = (string)xElement.Attribute("Variety");
                        string anchor  = (string)xElement.Attribute("Anchor");
                        curItem.Name     = $"{tileName} {variety} {anchor}";
                        curItem.SignId   = signId++;
                        curItem.UV       = StringToVector2Short((string)xElement.Attribute("UV"), 0, 0);
                        curItem.TileType = type;
                        SignProperties.Add(curItem);
                    }
                }
            }

            foreach (var xElement in xmlSettings.Elements("Npcs").Elements("Npc"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                NpcIds[name] = id;
                NpcNames[id] = name;
                var frames = StringToVector2Short((string)xElement.Attribute("Size"), 16, 40);
                NpcFrames[id] = frames;
            }

            foreach (var xElement in xmlSettings.Elements("ItemPrefix").Elements("Prefix"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                ItemPrefix.Add((byte)id, name);
            }

            foreach (var xElement in xmlSettings.Elements("ShortCutKeys").Elements("Shortcut"))
            {
                var key      = InLineEnumTryParse <Key>((string)xElement.Attribute("Key"));
                var modifier = InLineEnumTryParse <ModifierKeys>((string)xElement.Attribute("Modifier"));
                var tool     = (string)xElement.Attribute("Action");
                ShortcutKeys.Add(tool, key, modifier);
            }

            XElement appSettings   = xmlSettings.Element("App");
            int      appWidth      = (int?)appSettings.Attribute("Width") ?? 800;
            int      appHeight     = (int?)appSettings.Attribute("Height") ?? 600;
            int      clipboardSize = (int)XNA.MathHelper.Clamp((int?)appSettings.Attribute("ClipboardRenderSize") ?? 512, 64, 4096);

            _appSize = new Vector2(appWidth, appHeight);
            ClipboardBuffer.ClipboardRenderSize = clipboardSize;

            ToolDefaultData.LoadSettings(xmlSettings.Elements("Tools"));

            AltC        = (string)xmlSettings.Element("AltC");
            SteamUserId = (int?)xmlSettings.Element("SteamUserId") ?? null;
        }
Example #7
0
 public ItemData(ItemPrefix prefix, ItemType type, int stackSize = 1)
 {
     this.Prefix    = prefix;
     this.Type      = type;
     this.StackSize = stackSize;
 }
Example #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ItemStack"/> structure with the specified item
 /// <paramref name="id"/> and optional <paramref name="prefix"/> and <paramref name="stackSize"/>.
 /// </summary>
 /// <param name="id">The item ID.</param>
 /// <param name="prefix">The item prefix.</param>
 /// <param name="stackSize">The item stack size.</param>
 public ItemStack(ItemId id, ItemPrefix prefix = ItemPrefix.None, short stackSize = 1)
 {
     Id        = id;
     Prefix    = prefix;
     StackSize = stackSize;
 }
Example #9
0
 public ItemData(ItemType type, int stackSize = 1)
 {
     this.prefix    = ItemPrefix.None;
     this.type      = type;
     this.stackSize = stackSize;
 }
Example #10
0
 public ItemData(ItemPrefix prefix, ItemType type, int stackSize)
 {
     this.prefix    = prefix;
     this.type      = type;
     this.stackSize = stackSize;
 }
Example #11
0
 public ItemData(ItemType type, int stackSize = 1)
 {
     this.prefix = ItemPrefix.None;
       this.type = type;
       this.stackSize = stackSize;
 }
Example #12
0
 public ItemData(ItemPrefix prefix, ItemType type, int stackSize)
 {
     this.prefix = prefix;
       this.type = type;
       this.stackSize = stackSize;
 }
Example #13
0
        private static void LoadObjectDbXml(string file)
        {
            var xmlSettings = XElement.Load(file);

            // Load Colors
            foreach (var xElement in xmlSettings.Elements("GlobalColors").Elements("GlobalColor"))
            {
                string    name  = (string)xElement.Attribute("Name");
                XNA.Color color = XnaColorFromString((string)xElement.Attribute("Color"));
                GlobalColors.Add(name, color);
            }

            foreach (var xElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                var curTile = new TileProperty();

                // Read XML attributes
                curTile.Color       = ColorFromString((string)xElement.Attribute("Color"));
                curTile.Name        = (string)xElement.Attribute("Name");
                curTile.Id          = (int?)xElement.Attribute("Id") ?? 0;
                curTile.IsFramed    = (bool?)xElement.Attribute("Framed") ?? false;
                curTile.IsSolid     = (bool?)xElement.Attribute("Solid") ?? false;
                curTile.IsSolidTop  = (bool?)xElement.Attribute("SolidTop") ?? false;
                curTile.IsLight     = (bool?)xElement.Attribute("Light") ?? false;
                curTile.FrameSize   = StringToVector2Short((string)xElement.Attribute("Size"), 1, 1);
                curTile.Placement   = InLineEnumTryParse <FramePlacement>((string)xElement.Attribute("Placement"));
                curTile.TextureGrid = StringToVector2Short((string)xElement.Attribute("TextureGrid"), 16, 16);
                curTile.IsGrass     = "Grass".Equals((string)xElement.Attribute("Special"));    /* Heathtech */
                curTile.IsPlatform  = "Platform".Equals((string)xElement.Attribute("Special")); /* Heathtech */
                curTile.IsCactus    = "Cactus".Equals((string)xElement.Attribute("Special"));   /* Heathtech */
                curTile.IsStone     = (bool?)xElement.Attribute("Stone") ?? false;              /* Heathtech */
                curTile.CanBlend    = (bool?)xElement.Attribute("Blends") ?? false;             /* Heathtech */
                curTile.MergeWith   = (int?)xElement.Attribute("MergeWith") ?? null;            /* Heathtech */
                foreach (var elementFrame in xElement.Elements("Frames").Elements("Frame"))
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = (string)elementFrame.Attribute("Name");
                    curFrame.Variety = (string)elementFrame.Attribute("Variety");
                    curFrame.UV      = StringToVector2Short((string)elementFrame.Attribute("UV"), 0, 0);
                    curFrame.Anchor  = InLineEnumTryParse <FrameAnchor>((string)elementFrame.Attribute("Anchor"));

                    // Assign a default name if none existed
                    if (string.IsNullOrWhiteSpace(curFrame.Name))
                    {
                        curFrame.Name = curTile.Name;
                    }

                    curTile.Frames.Add(curFrame);
                    Sprites.Add(new Sprite
                    {
                        Anchor           = curFrame.Anchor,
                        IsPreviewTexture = false,
                        Name             = curFrame.Name + ", " + curFrame.Variety,
                        Origin           = curFrame.UV,
                        Size             = curTile.FrameSize,
                        Tile             = (byte)curTile.Id,
                        TileName         = curTile.Name
                    });
                }
                if (curTile.Frames.Count == 0 && curTile.IsFramed)
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = curTile.Name;
                    curFrame.Variety = string.Empty;
                    curFrame.UV      = new Vector2Short(0, 0);
                    //curFrame.Anchor = InLineEnumTryParse<FrameAnchor>((string)xElement.Attribute("Anchor"));

                    // Assign a default name if none existed
                    if (string.IsNullOrWhiteSpace(curFrame.Name))
                    {
                        curFrame.Name = curTile.Name;
                    }

                    curTile.Frames.Add(curFrame);
                    Sprites.Add(new Sprite
                    {
                        Anchor           = curFrame.Anchor,
                        IsPreviewTexture = false,
                        Name             = curFrame.Name + ", " + curFrame.Variety,
                        Origin           = curFrame.UV,
                        Size             = curTile.FrameSize,
                        Tile             = (byte)curTile.Id,
                        TileName         = curTile.Name
                    });
                }
                TileProperties.Add(curTile);
                if (!curTile.IsFramed)
                {
                    TileBricks.Add(curTile);
                }
            }
            for (int i = TileProperties.Count; i < 255; i++)
            {
                TileProperties.Add(new TileProperty(i, "UNKNOWN", Color.FromArgb(255, 255, 0, 255), true));
            }

            foreach (var xElement in xmlSettings.Elements("Walls").Elements("Wall"))
            {
                var curWall = new WallProperty();
                curWall.Color   = ColorFromString((string)xElement.Attribute("Color"));
                curWall.Name    = (string)xElement.Attribute("Name");
                curWall.Id      = (int?)xElement.Attribute("Id") ?? -1;
                curWall.IsHouse = (bool?)xElement.Attribute("IsHouse") ?? false;
                WallProperties.Add(curWall);
            }

            foreach (var xElement in xmlSettings.Elements("Items").Elements("Item"))
            {
                var curItem = new ItemProperty();
                curItem.Id   = (int?)xElement.Attribute("Id") ?? -1;
                curItem.Name = (string)xElement.Attribute("Name");
                ItemProperties.Add(curItem);
                _itemLookup.Add(curItem.Id, curItem);
            }

            foreach (var xElement in xmlSettings.Elements("Npcs").Elements("Npc"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                NpcIds.Add(name, id);
                int frames = (int?)xElement.Attribute("Frames") ?? 16;
                NpcFrames.Add(id, frames);
            }

            foreach (var xElement in xmlSettings.Elements("ItemPrefix").Elements("Prefix"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                ItemPrefix.Add((byte)id, name);
            }

            foreach (var xElement in xmlSettings.Elements("ShortCutKeys").Elements("Shortcut"))
            {
                var key  = InLineEnumTryParse <Key>((string)xElement.Attribute("Key"));
                var tool = (string)xElement.Attribute("Tool");
                ShortcutKeys.Add(key, tool);
            }

            XElement appSettings = xmlSettings.Element("App");
            int      appWidth    = (int?)appSettings.Attribute("Width") ?? 800;
            int      appHeight   = (int?)appSettings.Attribute("Height") ?? 600;

            _appSize = new Vector2(appWidth, appHeight);

            ToolDefaultData.LoadSettings(xmlSettings.Elements("Tools"));

            AltC = (string)xmlSettings.Element("AltC");
        }
Example #14
0
 public string GetItemPrefixName(ItemPrefix prefix)
 {
     return(Lang.prefix[(int)prefix]);
 }
Example #15
0
        //private static IEnumerable<TOut> StringToList<TOut>(string xmlcsv)
        //{
        //	if( !string.IsNullOrWhiteSpace(xmlcsv) )
        //	{
        //		string[] split = xmlcsv.Split(',');
        //		foreach( var s in split )
        //		{
        //			yield return (TOut)Convert.ChangeType(s, typeof(TOut));
        //		}
        //	}
        //}

        //private static T InLineEnumTryParse<T>(string str) where T : struct
        //{
        //	T result;
        //	Enum.TryParse(str, true, out result);
        //	return result;
        //}

        //private static Vector2Short StringToVector2Short(string v, short defaultX = 0, short defaultY = 0)
        //{
        //	if( !string.IsNullOrWhiteSpace(v) )
        //	{

        //		short x = 0;
        //		short y = 0;
        //		var split = v.Split(',');
        //		if( split.Length == 2 )
        //		{
        //			if( short.TryParse(split[0], out x) && short.TryParse(split[1], out y) )
        //				return new Vector2Short(x, y);
        //		}
        //	}

        //	return new Vector2Short(defaultX, defaultY);
        //}

        //private static Color ColorFromString(string colorstring)
        //{
        //	if( !string.IsNullOrWhiteSpace(colorstring) )
        //	{
        //		var colorFromString = ColorConverter.ConvertFromString(colorstring);
        //		if( colorFromString != null )
        //		{
        //			return (Color)colorFromString;
        //		}
        //	}
        //	return Colors.Magenta;
        //}
        //private static XNA.Color XnaColorFromString(string colorstring)
        //{
        //	if( !string.IsNullOrWhiteSpace(colorstring) )
        //	{
        //		var colorFromString = ColorConverter.ConvertFromString(colorstring);
        //		if( colorFromString != null )
        //		{
        //			var c = (Color)colorFromString;
        //			return XNA.Color.FromNonPremultiplied(c.R, c.G, c.B, c.A);
        //		}
        //	}
        //	return XNA.Color.Magenta;
        //}

        private static void LoadObjectDbXml(string file)
        {
            var xmlSettings = XElement.Load(file);

            // Load Colors
            //foreach( var xElement in xmlSettings.Elements("GlobalColors").Elements("GlobalColor") )
            //{
            //	string name = (string)xElement.Attribute("Name");
            //	XNA.Color color = XnaColorFromString((string)xElement.Attribute("Color"));
            //	GlobalColors.Add(name, color);
            //}

            foreach (var xElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                var curTile = new TileProperty();

                // Read XML attributes
                //curTile.Color = ColorFromString((string)xElement.Attribute("Color"));
                curTile.Name = (string)xElement.Attribute("Name");
                //curTile.Id = (int?)xElement.Attribute("Id") ?? 0;
                curTile.IsFramed = (bool?)xElement.Attribute("Framed") ?? false;
                curTile.IsSolid  = (bool?)xElement.Attribute("Solid") ?? false;
                //curTile.IsSolidTop = (bool?)xElement.Attribute("SolidTop") ?? false;
                //curTile.IsLight = (bool?)xElement.Attribute("Light") ?? false;
                //curTile.FrameSize = StringToVector2Short((string)xElement.Attribute("Size"), 1, 1);
                //curTile.Placement = InLineEnumTryParse<FramePlacement>((string)xElement.Attribute("Placement"));
                //curTile.TextureGrid = StringToVector2Short((string)xElement.Attribute("TextureGrid"), 16, 16);
                //curTile.IsGrass = "Grass".Equals((string)xElement.Attribute("Special")); /* Heathtech */
                //curTile.IsPlatform = "Platform".Equals((string)xElement.Attribute("Special")); /* Heathtech */
                //curTile.IsCactus = "Cactus".Equals((string)xElement.Attribute("Special")); /* Heathtech */
                //curTile.IsStone = (bool?)xElement.Attribute("Stone") ?? false; /* Heathtech */
                //curTile.CanBlend = (bool?)xElement.Attribute("Blends") ?? false; /* Heathtech */
                //curTile.MergeWith = (int?)xElement.Attribute("MergeWith") ?? null; /* Heathtech */
                //curTile.HasFrameName = curTile.IsFramed && ( (bool?)xElement.Attribute("UseFrameName") ?? false );
                //string frameNamePostfix = (string)xElement.Attribute("FrameNamePostfix") ?? null;

                //foreach( var elementFrame in xElement.Elements("Frames").Elements("Frame") )
                //{
                //	var curFrame = new FrameProperty();
                //	// Read XML attributes
                //	curFrame.Name = (string)elementFrame.Attribute("Name");
                //	curFrame.Variety = (string)elementFrame.Attribute("Variety");
                //	curFrame.UV = StringToVector2Short((string)elementFrame.Attribute("UV"), 0, 0);
                //	curFrame.Anchor = InLineEnumTryParse<FrameAnchor>((string)elementFrame.Attribute("Anchor"));
                //	var frameSize = StringToVector2Short((string)elementFrame.Attribute("FrameSize"), curTile.FrameSize.X, curTile.FrameSize.Y);

                //	// Assign a default name if none existed
                //	if( string.IsNullOrWhiteSpace(curFrame.Name) )
                //		curFrame.Name = curTile.Name;

                //	curTile.Frames.Add(curFrame);
                //	string spriteName = null;
                //	if( curFrame.Name == curTile.Name )
                //	{
                //		if( !string.IsNullOrWhiteSpace(curFrame.Variety) )
                //			spriteName += curFrame.Variety;
                //	}
                //	else
                //	{
                //		spriteName += curFrame.Name;
                //		if( !string.IsNullOrWhiteSpace(curFrame.Variety) )
                //			spriteName += " - " + curFrame.Variety;
                //	}
                //	Sprites.Add(new Sprite
                //	{
                //		Anchor = curFrame.Anchor,
                //		IsPreviewTexture = false,
                //		Name = spriteName,
                //		Origin = curFrame.UV,
                //		Size = frameSize,
                //		Tile = (ushort)curTile.Id, /* SBlogic */
                //		TileName = curTile.Name
                //	});
                //	if( curTile.HasFrameName )
                //	{
                //		string frameName = curFrame.Name;
                //		if( frameNamePostfix != null )
                //			frameName += " (" + frameNamePostfix + ")";
                //		if( curFrame.Variety != null )
                //			frameName += ", " + curFrame.Variety;

                //		//  TODO:  There must be a more efficient way than to store each frame...
                //		for( int x = 0, mx = curTile.FrameSize.X; x < mx; x++ )
                //		{
                //			for( int y = 0, my = curTile.FrameSize.Y; y < my; y++ )
                //			{
                //				string frameNameKey = GetFrameNameKey(curTile.Id, (short)( curFrame.UV.X + ( x * 18 ) ), (short)( curFrame.UV.Y + ( y * 18 ) ));
                //				if( !FrameNames.ContainsKey(frameNameKey) )
                //					FrameNames.Add(frameNameKey, frameName);
                //				else
                //					System.Diagnostics.Debug.WriteLine(curFrame.Name + " collided with " + frameNameKey);
                //			}
                //		}
                //	}
                //}
                if (curTile.Frames.Count == 0 && curTile.IsFramed)
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = curTile.Name;
                    curFrame.Variety = string.Empty;
                    curFrame.UV      = new Vector2Short(0, 0);
                    //curFrame.Anchor = InLineEnumTryParse<FrameAnchor>((string)xElement.Attribute("Anchor"));

                    curTile.Frames.Add(curFrame);
                    // not sure why sprites are added here. Disabling.
                    //Sprites.Add(new Sprite
                    //{
                    //	Anchor = curFrame.Anchor,
                    //	IsPreviewTexture = false,
                    //	Name = null,
                    //	Origin = curFrame.UV,
                    //	Size = curTile.FrameSize,
                    //	Tile = (ushort)curTile.Id,
                    //	TileName = curTile.Name
                    //});
                }
                TileProperties.Add(curTile);

                //???? disabled
                //if( !curTile.IsFramed )
                //	TileBricks.Add(curTile);
            }

            //???? disabled
            //for( int i = TileProperties.Count; i < 255; i++ )
            //{
            //	TileProperties.Add(new TileProperty(i, "UNKNOWN", Color.FromArgb(255, 255, 0, 255), true));
            //}

            //foreach( var xElement in xmlSettings.Elements("Walls").Elements("Wall") )
            //{
            //	var curWall = new WallProperty();
            //	curWall.Color = ColorFromString((string)xElement.Attribute("Color"));
            //	curWall.Name = (string)xElement.Attribute("Name");
            //	curWall.Id = (int?)xElement.Attribute("Id") ?? -1;
            //	WallProperties.Add(curWall);
            //}

            foreach (var xElement in xmlSettings.Elements("Items").Elements("Item"))
            {
                var curItem = new ItemProperty();
                curItem.Id    = (int?)xElement.Attribute("Id") ?? -1;
                curItem.Name  = (string)xElement.Attribute("Name");
                curItem.Scale = (float?)xElement.Attribute("Scale") ?? 1f;
                ItemProperties.Add(curItem);
                _itemLookup.Add(curItem.Id, curItem);
                int tally = (int?)xElement.Attribute("Tally") ?? 0;
                if (tally > 0)
                {
                    _tallynames.Add(tally, curItem.Name);
                }
                int head = (int?)xElement.Attribute("Head") ?? -1;
                if (head >= 0)
                {
                    _armorHeadNames.Add(head, curItem.Name);
                }
                int body = (int?)xElement.Attribute("Body") ?? -1;
                if (body >= 0)
                {
                    _armorBodyNames.Add(body, curItem.Name);
                }
                int legs = (int?)xElement.Attribute("Legs") ?? -1;
                if (legs >= 0)
                {
                    _armorLegsNames.Add(legs, curItem.Name);
                }
                bool rack = (bool?)xElement.Attribute("Rack") ?? false;
                if (rack)
                {
                    _rackable.Add(curItem.Id, curItem.Name);
                }
            }

            //foreach( var xElement in xmlSettings.Elements("Paints").Elements("Paint") )
            //{
            //	var curPaint = new PaintProperty();
            //	curPaint.Id = (int?)xElement.Attribute("Id") ?? -1;
            //	curPaint.Name = (string)xElement.Attribute("Name");
            //	curPaint.Color = ColorFromString((string)xElement.Attribute("Color"));
            //	PaintProperties.Add(curPaint);
            //}

            //int chestId = 0;
            //foreach( var tileElement in xmlSettings.Elements("Tiles").Elements("Tile") )
            //{
            //	string tileName = (string)tileElement.Attribute("Name");
            //	if( tileName == "Chest" || tileName == "Dresser" )
            //	{
            //		ushort type = (ushort)( (int?)tileElement.Attribute("Id") ?? 21 );
            //		foreach( var xElement in tileElement.Elements("Frames").Elements("Frame") )
            //		{
            //			var curItem = new ChestProperty();
            //			curItem.Name = (string)xElement.Attribute("Name");
            //			string variety = (string)xElement.Attribute("Variety");
            //			if( variety != null )
            //			{
            //				if( tileName == "Dresser" )
            //				{
            //					curItem.Name = variety + " " + "Dresser";
            //				}
            //				else
            //				{
            //					curItem.Name = curItem.Name + " " + variety;
            //				}
            //			}
            //			curItem.ChestId = chestId++;
            //			curItem.UV = StringToVector2Short((string)xElement.Attribute("UV"), 0, 0);
            //			curItem.TileType = type;
            //			ChestProperties.Add(curItem);
            //		}
            //	}
            //}

            //int signId = 0;
            //foreach( var tileElement in xmlSettings.Elements("Tiles").Elements("Tile") )
            //{
            //	string tileName = (string)tileElement.Attribute("Name");
            //	if( tileName == "Sign" || tileName == "Grave Marker" || tileName == "Announcement Box" )
            //	{
            //		ushort type = (ushort)( (int?)tileElement.Attribute("Id") ?? 55 );
            //		foreach( var xElement in tileElement.Elements("Frames").Elements("Frame") )
            //		{
            //			var curItem = new SignProperty();
            //			string variety = (string)xElement.Attribute("Variety");
            //			if( variety != null )
            //			{
            //				if( tileName == "Sign" )
            //				{
            //					curItem.Name = "Sign " + variety;
            //				}
            //				else
            //				{
            //					curItem.Name = variety;
            //				}
            //			}
            //			curItem.SignId = signId++;
            //			curItem.UV = StringToVector2Short((string)xElement.Attribute("UV"), 0, 0);
            //			curItem.TileType = type;
            //			SignProperties.Add(curItem);
            //		}
            //	}
            //}

            //foreach( var xElement in xmlSettings.Elements("Npcs").Elements("Npc") )
            //{
            //	int id = (int?)xElement.Attribute("Id") ?? -1;
            //	string name = (string)xElement.Attribute("Name");
            //	NpcIds.Add(name, id);
            //	NpcNames.Add(id, name);
            //	int frames = (int?)xElement.Attribute("Frames") ?? 16;
            //	NpcFrames.Add(id, frames);
            //}

            foreach (var xElement in xmlSettings.Elements("ItemPrefix").Elements("Prefix"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                ItemPrefix.Add((byte)id, name);
            }

            //foreach( var xElement in xmlSettings.Elements("ShortCutKeys").Elements("Shortcut") )
            //{
            //	var key = InLineEnumTryParse<Key>((string)xElement.Attribute("Key"));
            //	var tool = (string)xElement.Attribute("Tool");
            //	ShortcutKeys.Add(key, tool);
            //}

            //XElement appSettings = xmlSettings.Element("App");
            //int appWidth = (int?)appSettings.Attribute("Width") ?? 800;
            //int appHeight = (int?)appSettings.Attribute("Height") ?? 600;
            //int clipboardSize = (int)XNA.MathHelper.Clamp((int?)appSettings.Attribute("ClipboardRenderSize") ?? 512, 64, 4096);

            //_appSize = new Vector2(appWidth, appHeight);
            //ClipboardBuffer.ClipboardRenderSize = clipboardSize;

            //ToolDefaultData.LoadSettings(xmlSettings.Elements("Tools"));

            //AltC = (string)xmlSettings.Element("AltC");
            //SteamUserId = (int?)xmlSettings.Element("SteamUserId") ?? null;
        }
Example #16
0
        private void NetHooks_GetData(GetDataEventArgs e)
        {
            if (e == null || this.isDisposed || e.Handled)
            {
                return;
            }

            TSPlayer player = TShock.Players[e.Msg.whoAmI];

            if (player == null)
            {
                return;
            }

            try {
                switch (e.MsgID)
                {
                case PacketTypes.Tile: {
                    if (this.TileEdit == null)
                    {
                        break;
                    }

                    int editType = e.Msg.readBuffer[e.Index];
                    int x        = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 1);
                    int y        = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 3);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y) || editType > 14)
                    {
                        return;
                    }

                    int blockType   = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 5);
                    int objectStyle = e.Msg.readBuffer[e.Index + 7];

                    e.Handled = this.OnTileEdit(
                        new TileEditEventArgs(player, (TileEditType)editType, new DPoint(x, y), (BlockType)blockType, objectStyle
                                              ));
                    break;
                }

                case PacketTypes.PlaceObject: {
                    if (this.ObjectPlacement == null && this.TileEdit == null)
                    {
                        break;
                    }

                    int x = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int y = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y))
                    {
                        return;
                    }

                    int  blockType   = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 4);
                    int  objectStyle = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 6);
                    int  alternative = e.Msg.readBuffer[e.Index + 8];
                    int  random      = ((sbyte)e.Msg.readBuffer[e.Index + 9]);
                    bool direction   = BitConverter.ToBoolean(e.Msg.readBuffer, e.Index + 10);

                    if (this.InvokeTileOnObjectPlacement)
                    {
                        e.Handled = this.OnTileEdit(
                            new TileEditEventArgs(player, TileEditType.PlaceTile, new DPoint(x, y), (BlockType)blockType, objectStyle
                                                  ));
                    }

                    if (!e.Handled)
                    {
                        e.Handled = this.OnObjectPlacement(
                            new ObjectPlacementEventArgs(player, new DPoint(x, y), (BlockType)blockType, objectStyle, alternative, random, direction
                                                         ));
                    }

                    break;
                }

                // Note: As for TileKill and TileKillNoItem, blockId will be of "1" if the player attempted to destroy
                // a tile but didn't succeed yet, and will be of "0" as the tile is actually destroyed.
                // However, there's one exception with Chests, they will never send their actual destroy packet, except a hack
                // tool is used, it seems.
                case PacketTypes.TileKill: {
                    int type = e.Msg.readBuffer[e.Index];
                    int x    = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 1);
                    int y    = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 3);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y))
                    {
                        break;
                    }

                    int style = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 5);

                    if (type == 0 || type == 2) // Chest placement / Dresser Placement
                    {
                        e.Handled = this.OnChestPlace(new ChestPlaceEventArgs(player, new DPoint(x, y), type, style));
                    }
                    else // Chest kill
                    {
                        int tileType = TerrariaUtils.Tiles[x, y].type;
                        if (tileType != TileID.Containers && tileType != TileID.Dressers)
                        {
                            break;
                        }

                        if (this.InvokeTileEditOnChestKill)
                        {
                            e.Handled = this.OnTileEdit(new TileEditEventArgs(player, TileEditType.TileKill, new DPoint(x, y), 0, 0));
                        }

                        if (!e.Handled)
                        {
                            e.Handled = this.OnChestKill(new TileLocationEventArgs(player, new DPoint(x, y)));
                        }
                    }

                    break;
                }

                case PacketTypes.ChestOpen: {
                    if (this.ChestOpen == null && this.ChestRename == null)
                    {
                        break;
                    }

                    int chestIndex = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int x          = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);
                    int y          = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 4);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y))
                    {
                        break;
                    }

                    int nameLength = e.Msg.readBuffer[e.Index + 6];

                    string newName = string.Empty;
                    if ((nameLength > 0 && nameLength <= 20) || nameLength == 255) // Name change requested?
                    {
                        if (nameLength != 255)
                        {
                            newName = Encoding.UTF8.GetString(e.Msg.readBuffer, e.Index + 8, nameLength);
                        }

                        e.Handled = this.OnChestRename(new ChestRenameEventArgs(player, chestIndex, newName));
                    }

                    if (!e.Handled)
                    {
                        e.Handled = this.OnChestOpen(new ChestOpenEventArgs(player, chestIndex, new DPoint(x, y)));
                    }

                    break;
                }

                case PacketTypes.ChestGetContents: {
                    if (this.ChestGetContents == null)
                    {
                        break;
                    }

                    int x = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int y = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y) || !Main.tile[x, y].active())
                    {
                        return;
                    }

                    e.Handled = this.OnChestGetContents(new TileLocationEventArgs(player, new DPoint(x, y)));
                    break;
                }

                case PacketTypes.ChestItem: {
                    if (this.ChestModifySlot == null)
                    {
                        break;
                    }

                    int        chestIndex    = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int        slotIndex     = e.Msg.readBuffer[e.Index + 2];
                    int        itemStackSize = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 3);
                    ItemPrefix itemPrefix    = (ItemPrefix)e.Msg.readBuffer[e.Index + 5];
                    ItemType   itemType      = (ItemType)BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 6);

                    if (chestIndex >= Main.chest.Length || slotIndex > 39)
                    {
                        break;
                    }

                    e.Handled = this.OnChestModifySlot(new ChestModifySlotEventArgs(
                                                           player, chestIndex, slotIndex, new ItemData(itemPrefix, itemType, itemStackSize)
                                                           ));
                    break;
                }

                case PacketTypes.SignNew: {
                    if (this.SignEdit == null)
                    {
                        break;
                    }

                    int    signIndex = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int    x         = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);
                    int    y         = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 4);
                    string newText;
                    using (MemoryStream stream = new MemoryStream(e.Msg.readBuffer, e.Index + 6, e.Length - 7))
                        newText = new BinaryReader(stream).ReadString();

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y) || !Main.tile[x, y].active())
                    {
                        return;
                    }

                    e.Handled = this.OnSignEdit(new SignEditEventArgs(player, signIndex, new DPoint(x, y), newText));
                    break;
                }

                case PacketTypes.SignRead: {
                    if (this.SignRead == null)
                    {
                        break;
                    }

                    int x = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int y = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y))
                    {
                        break;
                    }

                    e.Handled = this.OnSignRead(new TileLocationEventArgs(player, new DPoint(x, y)));
                    break;
                }

                case PacketTypes.HitSwitch: {
                    if (this.HitSwitch == null)
                    {
                        break;
                    }

                    int x = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int y = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y) || !Main.tile[x, y].active())
                    {
                        return;
                    }

                    // For some reason, TShock doesn't handle this packet so we just do our own checks.
                    if (TShock.CheckIgnores(player))
                    {
                        return;
                    }
                    if (TShock.CheckRangePermission(player, x, y, 32))
                    {
                        return;
                    }

                    e.Handled = this.OnHitSwitch(new TileLocationEventArgs(player, new DPoint(x, y)));
                    break;
                }

                case PacketTypes.SpawnBossorInvasion: {
                    if (this.BossSpawn == null)
                    {
                        break;
                    }

                    //int playerIndex = BitConverter.ToInt32(e.Msg.readBuffer, e.Index);
                    int bossType = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);

                    e.Handled = this.OnBossSpawn(new BossSpawnEventArgs(player, (BossType)bossType));
                    break;
                }

                case PacketTypes.ItemDrop:
                case PacketTypes.UpdateItemDrop: { // ItemDrop2
                    if (this.ItemUpdate == null)
                    {
                        break;
                    }

                    int        itemIndex     = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    float      x             = BitConverter.ToSingle(e.Msg.readBuffer, e.Index + 2);
                    float      y             = BitConverter.ToSingle(e.Msg.readBuffer, e.Index + 6);
                    float      velocityX     = BitConverter.ToSingle(e.Msg.readBuffer, e.Index + 10);
                    float      velocityY     = BitConverter.ToSingle(e.Msg.readBuffer, e.Index + 14);
                    int        itemStackSize = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 18);
                    ItemPrefix itemPrefix    = (ItemPrefix)e.Msg.readBuffer[e.Index + 20];
                    bool       noDelay       = (e.Msg.readBuffer[e.Index + 21] != 0);
                    ItemType   itemType      = (ItemType)BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 22);

                    // If it is actually an item pick up, then ensure a valid item index.
                    if (itemType == 0 && (itemIndex < 0 || itemIndex >= Main.item.Length))
                    {
                        break;
                    }

                    e.Handled = this.OnItemUpdate(new ItemUpdateEventArgs(
                                                      player, itemIndex, new Vector2(x, y), new Vector2(velocityX, velocityY),
                                                      noDelay, new ItemData(itemPrefix, itemType, itemStackSize)
                                                      ));
                    break;
                }

                case PacketTypes.ItemOwner: {
                    if (this.ItemOwner == null)
                    {
                        break;
                    }

                    int      itemIndex           = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int      newOwnerPlayerIndex = e.Msg.readBuffer[e.Index + 2];
                    TSPlayer newOwner;
                    if (newOwnerPlayerIndex < 255)
                    {
                        newOwner = TShock.Players[newOwnerPlayerIndex];
                    }
                    else
                    {
                        break;
                    }

                    e.Handled = this.OnItemOwner(new ItemOwnerEventArgs(player, itemIndex, newOwner));
                    break;
                }

                case PacketTypes.ForceItemIntoNearestChest: { // QuickStackNearby
                    if (this.QuickStackNearby == null)
                    {
                        break;
                    }

                    int slotIndex = e.Msg.readBuffer[e.Index];
                    if (slotIndex >= TSPlayer.Server.TPlayer.inventory.Length)
                    {
                        break;
                    }

                    e.Handled = this.OnQuickStackNearby(new PlayerSlotEventArgs(player, slotIndex));
                    break;
                }

                case PacketTypes.PlayerSlot: {
                    if (this.PlayerModifySlot == null)
                    {
                        break;
                    }

                    //byte playerIndex = e.Msg.readBuffer[e.Index];
                    int        slotIndex     = e.Msg.readBuffer[e.Index + 1];
                    int        itemStackSize = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);
                    ItemPrefix itemPrefix    = (ItemPrefix)e.Msg.readBuffer[e.Index + 4];
                    ItemType   itemType      = (ItemType)BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 5);

                    Player tServerPlayer = TSPlayer.Server.TPlayer;
                    if (slotIndex >= tServerPlayer.inventory.Length + tServerPlayer.bank.item.Length + tServerPlayer.bank2.item.Length)
                    {
                        break;
                    }

                    e.Handled = this.OnPlayerModifySlot(new PlayerModifySlotEventArgs(
                                                            player, slotIndex, new ItemData(itemPrefix, itemType, itemStackSize)
                                                            ));
                    break;
                }

                case PacketTypes.LiquidSet: {
                    if (this.LiquidSet == null)
                    {
                        break;
                    }

                    int x = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int y = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y))
                    {
                        break;
                    }

                    int        liquidAmount = e.Msg.readBuffer[e.Index + 4];
                    LiquidKind liquidKind   = (LiquidKind)e.Msg.readBuffer[e.Index + 5];

                    e.Handled = this.OnLiquidSet(new LiquidSetEventArgs(player, new DPoint(x, y), liquidAmount, liquidKind));
                    break;
                }

                case PacketTypes.DoorUse: {
                    if (this.DoorUse == null)
                    {
                        break;
                    }

                    byte action = e.Msg.readBuffer[e.Index];
                    int  x      = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 1);
                    int  y      = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 3);

                    if (!TerrariaUtils.Tiles.IsValidCoord(x, y))
                    {
                        break;
                    }

                    int direction = e.Msg.readBuffer[e.Index + 5];

                    Direction actualDirection = Direction.Right;
                    if (direction == 0)
                    {
                        actualDirection = Direction.Left;
                    }

                    e.Handled = this.OnDoorUse(new DoorUseEventArgs(player, new DPoint(x, y), (DoorAction)action, actualDirection));
                    break;
                }

                case PacketTypes.PlayerSpawn: {
                    if (this.PlayerSpawn == null)
                    {
                        break;
                    }

                    int playerIndex = e.Msg.readBuffer[e.Index];
                    int spawnX      = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 1);
                    int spawnY      = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 3);

                    if (!TerrariaUtils.Tiles.IsValidCoord(spawnX, spawnY))
                    {
                        break;
                    }

                    e.Handled = this.OnPlayerSpawn(new PlayerSpawnEventArgs(player, new DPoint(spawnX, spawnY)));
                    break;
                }

                // Note: Also door unlock
                case PacketTypes.ChestUnlock: {
                    if (this.ChestUnlock == null)
                    {
                        break;
                    }

                    UnlockType unlockType = (UnlockType)e.Msg.readBuffer[e.Index];
                    int        chestX     = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 1);
                    int        chestY     = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 3);

                    if (!TerrariaUtils.Tiles.IsValidCoord(chestX, chestY))
                    {
                        break;
                    }

                    e.Handled = this.OnChestUnlock(new UnlockEventArgs(player, new DPoint(chestX, chestY), unlockType));
                    break;
                }

                case PacketTypes.ChatText: {
                    if (this.ChatText == null)
                    {
                        break;
                    }

                    short playerIndex = e.Msg.readBuffer[e.Index];
                    if (playerIndex != e.Msg.whoAmI)
                    {
                        break;
                    }

                    int    colorR = e.Msg.readBuffer[e.Index + 1];
                    int    colorG = e.Msg.readBuffer[e.Index + 2];
                    int    colorB = e.Msg.readBuffer[e.Index + 3];
                    string text   = Encoding.UTF8.GetString(e.Msg.readBuffer, e.Index + 4, e.Length - 5);

                    e.Handled = this.OnChatText(new ChatTextEventArgs(player, new Color(colorR, colorG, colorB), text));
                    break;
                }

                case PacketTypes.TileSendSquare: {
                    if (this.SendTileSquare == null)
                    {
                        break;
                    }

                    int size  = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int tileX = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);
                    int tileY = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 4);

                    if (!TerrariaUtils.Tiles.IsValidCoord(tileX, tileY))
                    {
                        break;
                    }

                    e.Handled = this.OnSendTileSquare(new SendTileSquareEventArgs(player, new DPoint(tileX, tileY), size));
                    break;
                }

                case PacketTypes.PaintTile: {
                    if (this.TilePaint == null)
                    {
                        break;
                    }

                    int tileX = BitConverter.ToInt16(e.Msg.readBuffer, e.Index);
                    int tileY = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);

                    if (!TerrariaUtils.Tiles.IsValidCoord(tileX, tileY))
                    {
                        break;
                    }

                    int color = e.Msg.readBuffer[e.Index + 8];

                    e.Handled = this.OnTilePaint(new TilePaintEventArgs(player, new DPoint(tileX, tileY), (PaintColor)color));
                    break;
                }

                case PacketTypes.PlayerKillMe: {
                    if (this.PlayerDeath == null)
                    {
                        break;
                    }

                    int    playerIndex = e.Msg.readBuffer[e.Index];
                    int    direction   = e.Msg.readBuffer[e.Index + 1];
                    int    dmg         = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 2);
                    bool   pvp         = e.Msg.readBuffer[e.Index + 4] != 0;
                    string deathText   = Encoding.UTF8.GetString(e.Msg.readBuffer, e.Index + 6, e.Length - 7);

                    e.Handled = this.OnPlayerDeath(new PlayerDeathEventArgs(player, direction, dmg, pvp, deathText));
                    break;
                }

                case PacketTypes.Teleport: {
                    if (this.Teleport == null)
                    {
                        break;
                    }

                    BitsByte flags        = e.Msg.readBuffer[e.Index];
                    int      playerIndex  = BitConverter.ToInt16(e.Msg.readBuffer, e.Index + 1);
                    float    x            = BitConverter.ToSingle(e.Msg.readBuffer, e.Index + 3);
                    float    y            = BitConverter.ToSingle(e.Msg.readBuffer, e.Index + 7);
                    Vector2  destLocation = new Vector2(x, y);

                    TeleportType tpType = TeleportType.PlayerToPos;
                    if (flags[0])
                    {
                        tpType = TeleportType.NpcToPos;
                    }
                    if (flags[1])
                    {
                        if (flags[0])
                        {
                            tpType = TeleportType.Unknown;
                        }
                        else
                        {
                            tpType = TeleportType.PlayerNearPlayerWormhole;
                        }
                    }

                    e.Handled = this.OnTeleport(new TeleportEventArgs(player, destLocation, tpType));
                    break;
                }
                }
            } catch (Exception ex) {
                ServerApi.LogWriter.PluginWriteLine(
                    this.Plugin, string.Format("Internal error on handling data packet {0}. Exception details: \n{1}", e.MsgID, ex), TraceLevel.Error
                    );
            }
        }
Example #17
0
        private static void LoadObjectDbXml(string file)
        {
            var xmlSettings = XElement.Load(file);

            // Load Colors
            foreach (var xElement in xmlSettings.Elements("GlobalColors").Elements("GlobalColor"))
            {
                string    name  = (string)xElement.Attribute("Name");
                XNA.Color color = XnaColorFromString((string)xElement.Attribute("Color"));
                GlobalColors.Add(name, color);
            }

            foreach (var xElement in xmlSettings.Elements("Tiles").Elements("Tile"))
            {
                var curTile = new TileProperty();

                // Read XML attributes
                curTile.Color       = ColorFromString((string)xElement.Attribute("Color"));
                curTile.Name        = (string)xElement.Attribute("Name");
                curTile.Id          = (int?)xElement.Attribute("Id") ?? 0;
                curTile.IsFramed    = (bool?)xElement.Attribute("Framed") ?? false;
                curTile.IsSolid     = (bool?)xElement.Attribute("Solid") ?? false;
                curTile.IsSolidTop  = (bool?)xElement.Attribute("SolidTop") ?? false;
                curTile.IsLight     = (bool?)xElement.Attribute("Light") ?? false;
                curTile.FrameSize   = StringToVector2Short((string)xElement.Attribute("Size"), 1, 1);
                curTile.Placement   = InLineEnumTryParse <FramePlacement>((string)xElement.Attribute("Placement"));
                curTile.TextureGrid = StringToVector2Short((string)xElement.Attribute("TextureGrid"), 16, 16);
                foreach (var elementFrame in xElement.Elements("Frames").Elements("Frame"))
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = (string)elementFrame.Attribute("Name");
                    curFrame.Variety = (string)elementFrame.Attribute("Variety");
                    curFrame.UV      = StringToVector2Short((string)elementFrame.Attribute("UV"), 0, 0);
                    curFrame.Anchor  = InLineEnumTryParse <FrameAnchor>((string)elementFrame.Attribute("Anchor"));

                    // Assign a default name if none existed
                    if (string.IsNullOrWhiteSpace(curFrame.Name))
                    {
                        curFrame.Name = curTile.Name;
                    }

                    curTile.Frames.Add(curFrame);
                    Sprites.Add(new Sprite
                    {
                        Anchor           = curFrame.Anchor,
                        IsPreviewTexture = false,
                        Name             = curFrame.Name + ", " + curFrame.Variety,
                        Origin           = curFrame.UV,
                        Size             = curTile.FrameSize,
                        Tile             = (byte)curTile.Id,
                        TileName         = curTile.Name
                    });
                    if (curTile.FrameSize.X == 0 && curTile.FrameSize.Y == 0)
                    {
                        int z = 0;
                    }
                }
                if (curTile.Frames.Count == 0 && curTile.IsFramed)
                {
                    var curFrame = new FrameProperty();
                    // Read XML attributes
                    curFrame.Name    = curTile.Name;
                    curFrame.Variety = string.Empty;
                    curFrame.UV      = new Vector2Short(0, 0);
                    //curFrame.Anchor = InLineEnumTryParse<FrameAnchor>((string)xElement.Attribute("Anchor"));

                    // Assign a default name if none existed
                    if (string.IsNullOrWhiteSpace(curFrame.Name))
                    {
                        curFrame.Name = curTile.Name;
                    }

                    curTile.Frames.Add(curFrame);
                    Sprites.Add(new Sprite
                    {
                        Anchor           = curFrame.Anchor,
                        IsPreviewTexture = false,
                        Name             = curFrame.Name + ", " + curFrame.Variety,
                        Origin           = curFrame.UV,
                        Size             = curTile.FrameSize,
                        Tile             = (byte)curTile.Id,
                        TileName         = curTile.Name
                    });
                }
                TileProperties.Add(curTile);
                if (!curTile.IsFramed)
                {
                    TileBricks.Add(curTile);
                }
            }
            for (int i = TileProperties.Count; i < 255; i++)
            {
                TileProperties.Add(new TileProperty(i, "UNKNOWN", Color.FromArgb(255, 255, 0, 255), true));
            }

            foreach (var xElement in xmlSettings.Elements("Walls").Elements("Wall"))
            {
                var curWall = new WallProperty();
                curWall.Color   = ColorFromString((string)xElement.Attribute("Color"));
                curWall.Name    = (string)xElement.Attribute("Name");
                curWall.Id      = (int?)xElement.Attribute("Id") ?? -1;
                curWall.IsHouse = (bool?)xElement.Attribute("IsHouse") ?? false;
                WallProperties.Add(curWall);
            }

            foreach (var xElement in xmlSettings.Elements("Items").Elements("Item"))
            {
                var curItem = new ItemProperty();
                curItem.Id   = (int?)xElement.Attribute("Id") ?? -1;
                curItem.Name = (string)xElement.Attribute("Name");
                ItemProperties.Add(curItem);
            }

            foreach (var xElement in xmlSettings.Elements("Npcs").Elements("Npc"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                NpcIds.Add(name, id);
            }

            foreach (var xElement in xmlSettings.Elements("ItemPrefix").Elements("Prefix"))
            {
                int    id   = (int?)xElement.Attribute("Id") ?? -1;
                string name = (string)xElement.Attribute("Name");
                ItemPrefix.Add((byte)id, name);
            }

            foreach (var xElement in xmlSettings.Elements("ShortCutKeys").Elements("Shortcut"))
            {
                var key  = InLineEnumTryParse <Key>((string)xElement.Attribute("Key"));
                var tool = (string)xElement.Attribute("Tool");
                ShortcutKeys.Add(key, tool);
            }
        }
Example #18
0
 /// <summary>
 /// Deconstructs the item stack.
 /// </summary>
 /// <param name="id">The item ID.</param>
 /// <param name="prefix">The item prefix.</param>
 /// <param name="stackSize">The item stack size.</param>
 public void Deconstruct(out ItemId id, out ItemPrefix prefix, out int stackSize)
 {
     id        = Id;
     stackSize = StackSize;
     prefix    = Prefix;
 }