public override Schematic Read(BinaryReader b, string name, int version)
        {
            var sizeX = b.ReadInt32();
            var sizeY = b.ReadInt32();
            var sch   = new Schematic(sizeX, sizeY);

            sch.Name  = name;
            sch.Tiles = World.LoadTileData(b, sizeX, sizeY);
            sch.Chests.AddRange(World.LoadChestData(b));
            sch.Signs.AddRange(World.LoadSignData(b));

            string verifyName    = b.ReadString();
            int    verifyVersion = b.ReadInt32();
            int    verifyX       = b.ReadInt32();
            int    verifyY       = b.ReadInt32();

            if (sch.Name == verifyName &&
                version == verifyVersion &&
                sch.Width == verifyX &&
                sch.Height == verifyY)
            {
                // valid;
                return(sch);
            }
            b.Close();

            return(null);
        }
Example #2
0
        /// <summary>
        /// Creates a Schematic, from existing tiles in the world.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="columns"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public static Schematic Grab(int x, int y, int columns, int rows)
        {
            //clip
            var worldRect     = new Rectangle(0, 0, Main.maxTilesX, Main.maxTilesY);
            var schematicRect = new Rectangle(x, y, columns, rows);
            var clippedRect   = schematicRect;

            worldRect.Intersects(ref clippedRect, out var intersects);

            var result = new Schematic(clippedRect.Width, clippedRect.Height);

            result.IsGrabbed = true;
            result.GrabbedX  = clippedRect.Left;
            result.GrabbedY  = clippedRect.Top;

            if (!intersects)
            {
                return(result);
            }

            //starting position within schematic to read from.
            var writeColumnStart = clippedRect.Left - x;
            var writeRowStart    = clippedRect.Top - y;

            var writeRow = writeRowStart;

            for (var row = clippedRect.Top; row < clippedRect.Bottom; row++)
            {
                var writeColumn = writeColumnStart;

                for (var column = clippedRect.Left; column < clippedRect.Right; column++)
                {
                    //var readTile = Tiles[readColumn, readRow];
                    var itile = Main.tile[column, row];
                    var tile  = new Tile();

                    tile.CopyFrom(itile);
                    result.Tiles[writeColumn, writeRow] = tile;

                    writeColumn++;
                }

                writeRow++;
            }

            //grab chests
            var chestIds = ChestFunctions.FindChests(clippedRect.Left, clippedRect.Top, clippedRect.Right, clippedRect.Bottom);

            foreach (var id in chestIds)
            {
                var srcChest = Main.chest[id];

                if (srcChest != null)
                {
                    Corruption.TEdit.Chest dst = new Corruption.TEdit.Chest(srcChest);

                    //we have to offset dst into coords that are relative to the schematic, not the world tileset.
                    dst.X = dst.X - x;
                    dst.Y = dst.Y - y;

                    result.Chests.Add(dst);
                }
            }


#if SCHEMATIC_ENABLE_SIGNS
            //grab signs

            var signIds = SignFunctions.FindSigns(clippedRect.Left, clippedRect.Top, clippedRect.Right, clippedRect.Bottom);
            foreach (var id in signIds)
            {
                var srcSign = Main.sign[id];

                if (srcSign != null)
                {
                    Corruption.TEdit.Sign dst = new Corruption.TEdit.Sign();

                    //we have to offset dst into coords that are relative to the schematic, not the world tileset.
                    dst.X    = srcSign.x - x;
                    dst.Y    = srcSign.y - y;
                    dst.Text = srcSign.text;

                    result.Signs.Add(dst);
                }
            }
#endif
            return(result);
        }
Example #3
0
        public override Schematic Read(BinaryReader b, string name, int version)
        {
            var tVersion  = (uint)version;
            var sizeX     = b.ReadInt32();
            var sizeY     = b.ReadInt32();
            var schematic = new Schematic(sizeX, sizeY, name);

            for (int x = 0; x < sizeX; ++x)
            {
                for (int y = 0; y < sizeY; y++)
                {
                    var tile = World.ReadTileDataFromStreamV1(b, tVersion);
                    // read complete, start compression
                    schematic.Tiles[x, y] = tile;

                    int rle = b.ReadInt16();
                    if (rle < 0)
                    {
                        throw new ApplicationException("Invalid Tile Data!");
                    }

                    if (rle > 0)
                    {
                        for (int k = y + 1; k < y + rle + 1; k++)
                        {
                            var tcopy = (Tile)tile.Clone();
                            schematic.Tiles[x, k] = tcopy;
                        }
                        y = y + rle;
                    }
                }
            }

            //throw new NotImplementedException("Chests disabled.");
            //schematic.Chests.Clear();
            //schematic.Chests.AddRange(World.ReadChestDataFromStreamV1(b, tVersion));

            //throw new NotImplementedException("Signs disabled.");
            //schematic.Signs.Clear();
            //foreach( Sign sign in World.ReadSignDataFromStreamV1(b) )
            //{
            //	if( schematic.Tiles[sign.X, sign.Y].IsActive && Tile.IsSign(schematic.Tiles[sign.X, sign.Y].Type) )
            //		schematic.Signs.Add(sign);
            //}

            string verifyName    = b.ReadString();
            int    verifyVersion = b.ReadInt32();
            int    verifyX       = b.ReadInt32();
            int    verifyY       = b.ReadInt32();

            if (schematic.Name == verifyName &&
                version == verifyVersion &&
                schematic.Width == verifyX &&
                schematic.Height == verifyY)
            {
                // valid;
                return(schematic);
            }
            b.Close();
            return(null);
        }