Esempio n. 1
0
        public ScenarioMap Expand(uint width, uint height, out RectangleMargins padding)
        {
            if (width < this.width)
            {
                throw new ArgumentOutOfRangeException(nameof(width));
            }

            if (height < this.height)
            {
                throw new ArgumentOutOfRangeException(nameof(height));
            }

            if (width == this.width && height == this.height)
            {
                throw new ArgumentException("Cannot expand when both width and height are same size as original.");
            }

            var map = new ScenarioMap();

            map.width   = width;
            map.height  = height;
            map.terrain = new Terrain[width, height];

            var padx = width - this.width;
            var pady = height - this.height;

            // padding priority goes to -x and +y due to camera angle
            var padright  = padx / 2;
            var padbottom = pady / 2;
            var padleft   = padx - padright;
            var padtop    = pady - padbottom;

            padding = new RectangleMargins((int)padleft, (int)padright, (int)padbottom, (int)padtop);

            padright = width - padright;
            padtop   = height - padtop;

            for (uint x = 0; x < width; x++)
            {
                for (uint y = 0; y < height; y++)
                {
                    var i = x < padleft ? 0 : x >= padright ? this.width - 1 : x - padleft;
                    var j = y < padbottom ? 0 : y >= padtop ? this.height - 1 : y - padbottom;

                    var originalTile = terrain[i, j];
                    map.terrain[x, y].cnst = originalTile.cnst;
                    map.terrain[x, y].elev = originalTile.elev;
                }
            }

            return(map);
        }
Esempio n. 2
0
        public Scenario(string path)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("File not found.", path);
            }

            MemoryStream decompressedData;

            using (var fileStream = File.Open(path, FileMode.Open, FileAccess.Read))
            {
                using (var binaryReader = new BinaryReader(fileStream))
                {
                    var version = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
                    FileVersion = version switch
                    {
                        "1.10" => AoeVersion.AOE1,
                        "1.11" => AoeVersion.AOE1,
                        "1.18" => AoeVersion.AOE2,
                        "1.21" => AoeVersion.AOE2TC,
                        _ => throw new Exception("Unknown scenario version: " + version),
                    };

                    var headerLength       = binaryReader.ReadInt32();
                    var checkSum           = binaryReader.ReadInt32();
                    var timeStamp          = binaryReader.ReadInt32();
                    var instructionsLength = binaryReader.ReadInt32();
                    var instructions       = binaryReader.ReadBytes(instructionsLength);
                    binaryReader.ReadInt32();
                    PlayerCount = binaryReader.ReadInt32();

                    // var compressedlength = binaryReader.BaseStream.Length - (headerLength + 8);

                    decompressedData          = Decompress(binaryReader);
                    decompressedData.Position = 0;
                }
            }

            using var reader = new BinaryReader(decompressedData);

            long next_uid = reader.ReadInt32();

            SubVersion = reader.ReadSingle();

            for (var i = 0; i < PlayerSlotCount; i++)
            {
                var name = reader.ReadBytes(256); // player name
            }

            if (FileVersion != AoeVersion.AOE1)
            {
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    reader.ReadInt32(); // string table
                }
            }

            PlayerData = new PlayerSlotData[PlayerSlotCount];
            for (var i = 0; i < PlayerSlotCount; i++)
            {
                PlayerData[i] = new PlayerSlotData(i, reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
            }

            reader.ReadInt32();
            reader.ReadByte();
            reader.ReadSingle();

            ScenarioName = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));

            var messageCount = (FileVersion != AoeVersion.AOE1 ? 6 : 5);

            long[]   mstrings = new long[6];
            string[] messages = new string[messageCount];
            string[] cinem    = new string[4];
            string[] unk      = new string[32];
            string[] ai       = new string[PlayerSlotCount];

            if (FileVersion != AoeVersion.AOE1)
            {
                for (int i = 0; i < 6; i++)
                {
                    mstrings[i] = reader.ReadInt32();
                }
            }

            for (var i = 0; i < messageCount; i++)
            {
                messages[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));
            }

            for (var i = 0; i < 4; i++)
            {
                cinem[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));
            }

            Messages = messages.ToImmutableArray();

            // bitmap
            var bitmap    = reader.ReadInt32();
            var bitmapx   = reader.ReadInt32();
            var bitmapy   = reader.ReadInt32();
            var bitmapunk = reader.ReadInt16();

            if (bitmap != 0)
            {
                // read some big ass bitmap file if bitmap != 0
                throw new NotSupportedException();
            }

            for (var i = 0; i < 32; i++)
            {
                unk[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));
            }

            for (var i = 0; i < PlayerSlotCount; i++)
            {
                ai[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));
            }

            for (var i = 0; i < PlayerSlotCount; i++)
            {
                var part1len    = reader.ReadInt32();
                var part2len    = reader.ReadInt32();
                var part3len    = reader.ReadUInt32();
                var totalLength = part1len + part2len + part3len;
                var text        = Encoding.ASCII.GetString(reader.ReadBytes((int)totalLength));
            }

            if (FileVersion != AoeVersion.AOE1)
            {
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    Console.WriteLine(i + ": aimode=" + reader.ReadByte());
                }
            }

            if (reader.ReadUInt32() != Sect)
            {
                throw new InvalidDataException();
            }

            // Resources
            for (var i = 0; i < PlayerSlotCount; i++)
            {
                PlayerData[i].SetStartingResources(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());

                if (FileVersion != AoeVersion.AOE1)
                {
                    Console.WriteLine("\tOrex: " + reader.ReadInt32());
                    Console.WriteLine("\tUnknown: " + reader.ReadInt32());
                }
            }

            if (reader.ReadUInt32() != Sect)
            {
                throw new InvalidDataException();
            }

            reader.ReadBytes(40); // victory stuff

            for (var i = 0; i < PlayerSlotCount; i++)
            {
                for (var j = 0; j < PlayerSlotCount; j++)
                {
                    var diplomacy = reader.ReadInt32(); // 0=ally?, 3=enemy?

                    /*Console.Write(diplomacy);
                     * if (j != (PlayerSlotCount - 1))
                     * {
                     *  Console.Write(" ");
                     * }
                     * else
                     * {
                     *  Console.WriteLine();
                     * }*/
                }
            }

            reader.ReadBytes(0x2D00); // idc

            if (reader.ReadUInt32() != Sect)
            {
                throw new InvalidDataException();
            }

            if (FileVersion == AoeVersion.AOE1)
            {
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    for (var j = 0; j < 21; j++)
                    {
                        reader.ReadInt32();
                    }
                }
            }
            else
            {
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    reader.ReadInt32();
                }
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    reader.ReadInt32();
                }
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    for (var j = 0; j < 30; j++)
                    {
                        reader.ReadInt32();
                    }
                }
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    reader.ReadInt32();
                }
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    for (var j = 0; j < 30; j++)
                    {
                        reader.ReadInt32();
                    }
                }
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    reader.ReadInt32();
                }
                for (var i = 0; i < PlayerSlotCount; i++)
                {
                    for (var j = 0; j < 20; j++)
                    {
                        reader.ReadInt32();
                    }
                }

                Console.WriteLine(reader.ReadInt32());
                Console.WriteLine(reader.ReadInt32());
            }

            var alltech = reader.ReadInt32() != 0;

            for (var i = 0; i < PlayerSlotCount; i++)
            {
                PlayerData[i].SetStartingAge(reader.ReadInt32());
            }

            var unk1 = reader.ReadUInt32();
            var unk2 = reader.ReadUInt32();

            if (reader.ReadUInt32() != Sect)
            {
                throw new InvalidDataException();
            }

            // Map
            Map = new ScenarioMap();
            Map.Read(reader, FileVersion);

            // Units
            // https://github.com/dderevjanik/agescx-js/blob/master/packages/io/Structures/UnitsStruct.ts
            var unitSections = reader.ReadInt32();

            if (unitSections != 9)
            {
                throw new Exception();
            }

            for (var i = 0; i < unitSections - 1; i++)
            {
                reader.ReadSingle();
                reader.ReadSingle();
                reader.ReadSingle();
                reader.ReadSingle();
            }

            Entities = new Dictionary <int, ImmutableArray <Entity> >();
            for (var i = 0; i < unitSections; i++)
            {
                var unitCount = reader.ReadUInt32();
                var units     = new Entity[unitCount];
                for (var j = 0; j < unitCount; j++)
                {
                    // var x = reader.ReadSingle();
                    var y = reader.ReadSingle();
                    var x = reader.ReadSingle();
                    if (x < 0 || x > Map.width || y < 0 || y > Map.height)
                    {
                        throw new InvalidDataException();
                    }

                    var unitunk    = reader.ReadSingle();
                    var identifier = reader.ReadUInt32();
                    var unittype   = (AoeEntityType)reader.ReadUInt16();
                    var status     = reader.ReadByte();
                    if (status != 2 && status != 7)
                    {
                        throw new InvalidDataException();
                    }

                    var rotation = reader.ReadSingle();
                    if (rotation < 0 || rotation > 23)
                    {
                        throw new InvalidDataException();
                    }

                    if (FileVersion != AoeVersion.AOE1)
                    {
                        var initialFrame = reader.ReadUInt16();
                        var garissonid   = reader.ReadInt32();
                    }

                    units[j] = new Entity(unittype, x, y, rotation, status, identifier);
                }

                Entities.Add(i, units.ToImmutableArray());
            }

            // more data...
        }
Esempio n. 3
0
        public Scenario(string path)
        {
            Console.WriteLine("Opening scenario: " + path);

            if (File.Exists(path))
            {
                using (m_BinaryReader = new BinaryReader(File.Open(path, FileMode.Open)))
                {
                    var version = Encoding.ASCII.GetString(m_BinaryReader.ReadBytes(4));

                    if (version == "1.18") // AOE2:AOK
                    {
                        FileVersion = AOEVersion.AOE2;
                    }
                    else if (version == "1.21") // AOE2:TC
                    {
                        FileVersion = AOEVersion.AOE2TC;
                    }
                    else if (version == "1.10" || version == "1.11") // AOE1
                    {
                        FileVersion = AOEVersion.AOE1;
                    }
                    else
                    {
                        throw new Exception("Unknown scenario version: " + version);
                    }

                    Console.WriteLine("Scenario Version: " + FileVersion);

                    var headerLength       = m_BinaryReader.ReadInt32();
                    var checkSum           = m_BinaryReader.ReadInt32();
                    var timeStamp          = m_BinaryReader.ReadInt32();
                    var instructionsLength = m_BinaryReader.ReadInt32();
                    var instructions       = m_BinaryReader.ReadBytes(instructionsLength);
                    m_BinaryReader.ReadInt32();
                    m_BinaryReader.ReadInt32(); // playerCount

                    var compressedlength = m_BinaryReader.BaseStream.Length - (headerLength + 8);
                    Console.WriteLine("Compressed Length: " + compressedlength);

                    byte[] outData = inflate_file(m_BinaryReader, compressedlength);

                    Console.WriteLine("Time to read the decompressed data.");

                    var   reader   = new BinaryReader(new MemoryStream(outData));
                    long  next_uid = reader.ReadInt32();
                    float version2 = reader.ReadSingle();

                    Console.WriteLine(version2);

                    for (int i = 0; i < 16; i++)
                    {
                        reader.ReadBytes(256); // player name
                    }
                    for (int i = 0; i < 16; i++)
                    {
                        reader.ReadInt32(); // string table
                    }
                    for (int i = 0; i < 16; i++)
                    {
                        Console.Write(i + ": ");
                        Console.Write(" Enabled: " + reader.ReadInt32());
                        Console.Write(" Human: " + reader.ReadInt32());
                        Console.Write(" Civ: " + reader.ReadInt32());
                        Console.Write(" Unk: " + reader.ReadInt32());
                        Console.WriteLine();
                    }

                    reader.ReadInt32();
                    reader.ReadByte();
                    reader.ReadSingle();

                    ushort origname = reader.ReadUInt16();
                    Console.WriteLine(origname);
                    Console.WriteLine(Encoding.ASCII.GetString(reader.ReadBytes(origname)));

                    long[]   mstrings = new long[6];
                    string[] messages = new string[6];
                    string[] cinem    = new string[4];
                    string[] unk      = new string[32];
                    string[] ai       = new string[16];

                    for (int i = 0; i < 6; i++)
                    {
                        mstrings[i] = reader.ReadInt32();
                    }

                    for (int i = 0; i < 6; i++)
                    {
                        messages[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));
                    }

                    for (int i = 0; i < 4; i++)
                    {
                        cinem[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));
                    }

                    // bitmap

                    var bitmap = reader.ReadInt32();

                    Console.WriteLine("bitmap: " + bitmap);
                    Console.WriteLine("x: " + reader.ReadInt32());
                    Console.WriteLine("y: " + reader.ReadInt32());
                    reader.ReadInt16(); // unknown

                    // read some big ass bitmap file if bitmap != 0

                    for (var i = 0; i < 32; i++)
                    {
                        unk[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));
                    }

                    for (int i = 0; i < 16; i++)
                    {
                        ai[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));
                    }

                    for (int i = 0; i < 16; i++)
                    {
                        Console.Write(i + ": ");
                        Console.Write(reader.ReadInt32() + " ");
                        Console.WriteLine(reader.ReadInt32());
                        Console.WriteLine(Encoding.ASCII.GetString(reader.ReadBytes((int)reader.ReadUInt32())));
                    }

                    for (int i = 0; i < 16; i++)
                    {
                        Console.WriteLine(i + ": aimode=" + reader.ReadByte());
                    }

                    Console.WriteLine(reader.ReadInt32().ToString("x"));

                    Console.WriteLine("Resources:");

                    for (int i = 0; i < 16; i++)
                    {
                        Console.WriteLine("Player " + i);
                        Console.WriteLine("\tGold: " + reader.ReadInt32());
                        Console.WriteLine("\tWood: " + reader.ReadInt32());
                        Console.WriteLine("\tFood: " + reader.ReadInt32());
                        Console.WriteLine("\tStone: " + reader.ReadInt32());
                        Console.WriteLine("\tOrex: " + reader.ReadInt32());
                        Console.WriteLine("\tUnknown: " + reader.ReadInt32());
                    }


                    Console.WriteLine(reader.ReadInt32().ToString("x"));

                    reader.ReadBytes(40); // victory stuff

                    for (int i = 0; i < 16; i++)
                    {
                        for (int j = 0; j < 16; j++)
                        {
                            reader.ReadInt32();
                        }
                    }
                    //Console.WriteLine(i + " - " + j + ": diplomacy=" + reader.ReadInt32());

                    reader.ReadBytes(0x2D00); // idc

                    Console.WriteLine(reader.ReadInt32().ToString("x"));

                    reader.ReadBytes(4 * 16); // idc

                    for (int i = 0; i < 16; i++)
                    {
                        reader.ReadInt32();
                    }
                    for (int i = 0; i < 16; i++)
                    {
                        for (int j = 0; j < 30; j++)
                        {
                            reader.ReadInt32();
                        }
                    }
                    for (int i = 0; i < 16; i++)
                    {
                        reader.ReadInt32();
                    }
                    for (int i = 0; i < 16; i++)
                    {
                        for (int j = 0; j < 30; j++)
                        {
                            reader.ReadInt32();
                        }
                    }
                    for (int i = 0; i < 16; i++)
                    {
                        reader.ReadInt32();
                    }
                    for (int i = 0; i < 16; i++)
                    {
                        for (int j = 0; j < 20; j++)
                        {
                            reader.ReadInt32();
                        }
                    }

                    reader.ReadInt32();
                    reader.ReadInt32();

                    Console.WriteLine("alltech: " + reader.ReadInt32());

                    for (int i = 0; i < 16; i++)
                    {
                        Console.WriteLine("Player " + i + " age: " + reader.ReadInt32());
                    }

                    // Map

                    Console.WriteLine(reader.ReadInt32().ToString("x"));

                    Console.WriteLine("Actual Map Stuff Now: ");
                    Console.WriteLine("CameraY: " + reader.ReadInt32());
                    Console.WriteLine("CameraX: " + reader.ReadInt32());

                    Map = new ScenarioMap();
                    Map.Read(reader, FileVersion);
                }
            }
            else
            {
                throw new FileNotFoundException("File not found.", path);
            }
        }
Esempio n. 4
0
        public Scenario(string path)
        {
            Console.WriteLine("Opening scenario: " + path);

            if (File.Exists(path))
            {
                using (m_BinaryReader = new BinaryReader(File.Open(path, FileMode.Open)))
                {
                    var version = Encoding.ASCII.GetString(m_BinaryReader.ReadBytes(4));

                    if (version == "1.18") // AOE2:AOK
                        FileVersion = AOEVersion.AOE2;
                    else if (version == "1.21") // AOE2:TC
                        FileVersion = AOEVersion.AOE2TC;
                    else if (version == "1.10" || version == "1.11") // AOE1
                        FileVersion = AOEVersion.AOE1;
                    else
                        throw new Exception("Unknown scenario version: " + version);

                    Console.WriteLine("Scenario Version: " + FileVersion);

                    var headerLength = m_BinaryReader.ReadInt32();
                    var checkSum = m_BinaryReader.ReadInt32();
                    var timeStamp = m_BinaryReader.ReadInt32();
                    var instructionsLength = m_BinaryReader.ReadInt32();
                    var instructions = m_BinaryReader.ReadBytes(instructionsLength);
                    m_BinaryReader.ReadInt32();
                    m_BinaryReader.ReadInt32(); // playerCount

                    var compressedlength = m_BinaryReader.BaseStream.Length - ( headerLength + 8 );
                    Console.WriteLine("Compressed Length: " + compressedlength);

                    byte[] outData = inflate_file(m_BinaryReader, compressedlength);

                    Console.WriteLine("Time to read the decompressed data.");

                    var reader = new BinaryReader(new MemoryStream(outData));
                    long next_uid = reader.ReadInt32();
                    float version2 = reader.ReadSingle();

                    Console.WriteLine(version2);

                    for (int i = 0; i < 16; i++)
                        reader.ReadBytes(256); // player name

                    for (int i = 0; i < 16; i++)
                        reader.ReadInt32(); // string table

                    for (int i = 0; i < 16; i++)
                    {
                        Console.Write(i + ": ");
                        Console.Write(" Enabled: " + reader.ReadInt32());
                        Console.Write(" Human: " + reader.ReadInt32());
                        Console.Write(" Civ: " + reader.ReadInt32());
                        Console.Write(" Unk: " + reader.ReadInt32());
                        Console.WriteLine();
                    }

                    reader.ReadInt32();
                    reader.ReadByte();
                    reader.ReadSingle();

                    ushort origname = reader.ReadUInt16();
                    Console.WriteLine(origname);
                    Console.WriteLine(Encoding.ASCII.GetString(reader.ReadBytes(origname)));

                    long[] mstrings = new long[6];
                    string[] messages = new string[6];
                    string[] cinem = new string[4];
                    string[] unk = new string[32];
                    string[] ai = new string[16];

                    for (int i = 0; i < 6; i++)
                        mstrings[i] = reader.ReadInt32();

                    for (int i = 0; i < 6; i++)
                        messages[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));

                    for (int i = 0; i < 4; i++)
                        cinem[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));

                    // bitmap

                    var bitmap = reader.ReadInt32();

                    Console.WriteLine("bitmap: " + bitmap);
                    Console.WriteLine("x: " + reader.ReadInt32());
                    Console.WriteLine("y: " + reader.ReadInt32());
                    reader.ReadInt16(); // unknown

                    // read some big ass bitmap file if bitmap != 0

                    for (var i = 0; i < 32; i++)
                        unk[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));

                    for (int i = 0; i < 16; i++)
                        ai[i] = Encoding.ASCII.GetString(reader.ReadBytes(reader.ReadUInt16()));

                    for (int i = 0; i < 16; i++)
                    {
                        Console.Write(i + ": ");
                        Console.Write(reader.ReadInt32() + " ");
                        Console.WriteLine(reader.ReadInt32());
                        Console.WriteLine(Encoding.ASCII.GetString(reader.ReadBytes((int)reader.ReadUInt32())));
                    }

                    for (int i = 0; i < 16; i++)
                        Console.WriteLine(i + ": aimode=" + reader.ReadByte());

                    Console.WriteLine(reader.ReadInt32().ToString("x"));

                    Console.WriteLine("Resources:");

                    for (int i = 0; i < 16; i++)
                    {
                        Console.WriteLine("Player " + i);
                        Console.WriteLine("\tGold: " + reader.ReadInt32());
                        Console.WriteLine("\tWood: " + reader.ReadInt32());
                        Console.WriteLine("\tFood: " + reader.ReadInt32());
                        Console.WriteLine("\tStone: " + reader.ReadInt32());
                        Console.WriteLine("\tOrex: " + reader.ReadInt32());
                        Console.WriteLine("\tUnknown: " + reader.ReadInt32());
                    }

                    Console.WriteLine(reader.ReadInt32().ToString("x"));

                    reader.ReadBytes(40); // victory stuff

                    for (int i = 0; i < 16; i++)
                        for (int j = 0; j < 16; j++)
                            reader.ReadInt32();
                            //Console.WriteLine(i + " - " + j + ": diplomacy=" + reader.ReadInt32());

                    reader.ReadBytes(0x2D00); // idc

                    Console.WriteLine(reader.ReadInt32().ToString("x"));

                    reader.ReadBytes(4 * 16); // idc

                    for (int i = 0; i < 16; i++)
                        reader.ReadInt32();
                    for (int i = 0; i < 16; i++)
                        for (int j = 0; j < 30; j++)
                            reader.ReadInt32();
                    for (int i = 0; i < 16; i++)
                        reader.ReadInt32();
                    for (int i = 0; i < 16; i++)
                        for (int j = 0; j < 30; j++)
                            reader.ReadInt32();
                    for (int i = 0; i < 16; i++)
                        reader.ReadInt32();
                    for (int i = 0; i < 16; i++)
                        for (int j = 0; j < 20; j++)
                            reader.ReadInt32();

                    reader.ReadInt32();
                    reader.ReadInt32();

                    Console.WriteLine("alltech: " + reader.ReadInt32());

                    for (int i = 0; i < 16; i++)
                        Console.WriteLine("Player " + i + " age: " + reader.ReadInt32());

                    // Map

                    Console.WriteLine(reader.ReadInt32().ToString("x"));

                    Console.WriteLine("Actual Map Stuff Now: ");
                    Console.WriteLine("CameraY: " + reader.ReadInt32());
                    Console.WriteLine("CameraX: " + reader.ReadInt32());

                    Map = new ScenarioMap();
                    Map.Read(reader, FileVersion);
                }
            }
            else
            {
                throw new FileNotFoundException("File not found.", path);
            }
        }