Example #1
0
        private void ReadPredefinedHeroes(BinaryReader reader)
        {
            mapObject.PredefinedHeroes = new List <HeroInstance>();
            if (mapObject.Header.Version == EMapFormat.WOG || mapObject.Header.Version == EMapFormat.SOD)
            {
                for (int z = 0; z < GameConstants.HEROES_QUANTITY; z++)
                {
                    Console.WriteLine(string.Format("===Reading Predefined Hero [{0}]", z));

                    int custom = reader.ReadByte();
                    if (custom == 0)
                    {
                        Console.WriteLine("is not custom.");
                        continue;
                    }

                    // Create Hero
                    HeroInstance hero = new HeroInstance();
                    hero.ObjectType = EObjectType.HERO;
                    hero.SubId      = z;

                    bool hasExp = reader.ReadBoolean();
                    if (hasExp)
                    {
                        hero.Data.Experience = (int)reader.ReadUInt32();
                        Console.WriteLine("Has exp:" + hero.Data.Experience);
                    }

                    hero.Data.SecondarySkills = new List <AbilitySkill>();
                    bool hasSecondSkills = reader.ReadBoolean();
                    if (hasSecondSkills)
                    {
                        uint howMany = reader.ReadUInt32();
                        Console.WriteLine("Has Second Skills count=" + howMany);

                        for (int yy = 0; yy < howMany; ++yy)
                        {
                            int first  = reader.ReadByte();
                            int second = reader.ReadByte();
                            Console.WriteLine(string.Format("Skill First: {0} Second: {1}", first, second));
                            AbilitySkill skill = new AbilitySkill((ESecondarySkill)first, (ESecondarySkillLevel)second);
                            hero.Data.SecondarySkills.Add(skill);
                        }
                    }

                    // Set Artifacts
                    CGHeroReader.LoadArtifactsOfHero(reader, mapObject, hero);

                    bool hasCustomBio = reader.ReadBoolean();
                    if (hasCustomBio)
                    {
                        string biography = reader.ReadStringWithLength();
                        Console.WriteLine("biography: " + biography);

                        hero.Data.Biography = biography;
                    }

                    byte sex = reader.ReadByte();
                    Console.WriteLine("sex: " + sex);
                    hero.Data.Sex = sex;

                    // Spells
                    bool hasCustomSpells = reader.ReadBoolean();
                    if (hasCustomSpells)
                    {
                        HashSet <int> spells = new HashSet <int>();
                        reader.ReadBitMask(spells, 9, GameConstants.SPELLS_QUANTITY, false);
                        Console.WriteLine("Spells: " + JsonConvert.SerializeObject(spells));

                        hero.Data.Spells = new List <ESpellId>();
                        foreach (int spell in spells)
                        {
                            hero.Data.Spells.Add((ESpellId)spell);
                        }
                    }

                    bool hasCustomPrimSkills = reader.ReadBoolean();
                    if (hasCustomPrimSkills)
                    {
                        Console.WriteLine("Has Custom Primary Skills.");

                        hero.Data.PrimarySkills = new List <int>();
                        for (int xx = 0; xx < GameConstants.PRIMARY_SKILLS; xx++)
                        {
                            int value = reader.ReadByte();
                            Console.WriteLine("Primary Skills: " + value);
                            hero.Data.PrimarySkills.Add(value);
                        }
                    }

                    mapObject.PredefinedHeroes.Add(hero);
                }
            }
        }
Example #2
0
        public override CGObject ReadObject(BinaryReader reader, int objectId, MapPosition objectPosition)
        {
            HeroInstance hero = new HeroInstance();

            if (MapHeader.Version > EMapFormat.ROE)
            {
                uint identifier = reader.ReadUInt32();
                //// map->questIdentifierToId[identifier] = objectId;
            }

            EPlayerColor owner = (EPlayerColor)reader.ReadByte();

            hero.SubId = reader.ReadByte();

            //If hero of this type has been predefined, use that as a base.
            //Instance data will overwrite the predefined values where appropriate.
            foreach (var preHero in this.Map.PredefinedHeroes)
            {
                if (preHero.SubId == hero.SubId)
                {
                    //logGlobal->debug("Hero %d will be taken from the predefined heroes list.", nhi->subID);
                    //delete nhi;
                    hero = preHero;
                    break;
                }
            }
            hero.SetOwner(owner);

            //// nhi->portrait = nhi->subID;
            foreach (DisposedHero disHero in this.Map.DisposedHeroes)
            {
                if (disHero.HeroId == hero.SubId)
                {
                    hero.Data.Name         = disHero.Name;
                    hero.Data.PortaitIndex = disHero.Portrait;
                    break;
                }
            }

            bool hasName = reader.ReadBoolean();

            if (hasName)
            {
                hero.Data.Name = reader.ReadStringWithLength();
            }

            if (MapHeader.Version > EMapFormat.AB)
            {
                bool hasExp = reader.ReadBoolean();
                if (hasExp)
                {
                    hero.Data.Experience = reader.ReadUInt32();
                }
                else
                {
                    hero.Data.Experience = 0xffffffff;
                }
            }
            else
            {
                hero.Data.Experience = reader.ReadUInt32();

                //0 means "not set" in <=AB maps
                if (hero.Data.Experience == 0)
                {
                    hero.Data.Experience = 0xffffffff;
                }
            }

            bool hasPortrait = reader.ReadBoolean();

            if (hasPortrait)
            {
                hero.Data.PortaitIndex = reader.ReadByte();
            }

            bool hasSecSkills = reader.ReadBoolean();

            if (hasSecSkills)
            {
                // Replacing with current data
                hero.Data.SecondarySkills = new List <AbilitySkill>();

                uint howMany = reader.ReadUInt32();
                for (int yy = 0; yy < howMany; ++yy)
                {
                    ESecondarySkill      skill = (ESecondarySkill)reader.ReadByte();
                    ESecondarySkillLevel level = (ESecondarySkillLevel)reader.ReadByte();

                    AbilitySkill abilitySkill = new AbilitySkill(skill, level);
                    hero.Data.SecondarySkills.Add(abilitySkill);
                }
            }

            bool hasGarison = reader.ReadBoolean();

            if (hasGarison)
            {
                hero.Data.Army = ReadCreatureSet(reader, 7);
                hero.Data.Army.FormationType = (EArmyFormationType)reader.ReadByte();
            }
            else
            {
                reader.ReadByte();
            }

            LoadArtifactsOfHero(reader, this.Map, hero);

            hero.Patrol = new HeroPatrol();
            hero.Patrol.PatrolRadius = reader.ReadByte();

            if (hero.Patrol.PatrolRadius == 0xff)
            {
                hero.Patrol.IsPatrolling = false;
            }
            else
            {
                hero.Patrol.IsPatrolling    = true;
                hero.Patrol.InitialPosition = objectPosition;
            }

            if (this.MapHeader.Version > EMapFormat.ROE)
            {
                bool hasCustomBiography = reader.ReadBoolean();
                if (hasCustomBiography)
                {
                    hero.Data.Biography = reader.ReadStringWithLength();
                }
                hero.Data.Sex = reader.ReadByte();

                // Remove trash
                if (hero.Data.Sex != 0xFF)
                {
                    hero.Data.Sex &= 1;
                }
            }
            else
            {
                hero.Data.Sex = 0xFF;
            }

            // Spells
            if (this.MapHeader.Version > EMapFormat.AB)
            {
                bool hasCustomSpells = reader.ReadBoolean();
                if (hasCustomSpells)
                {
                    ReadSpells(reader, hero);
                }
            }
            else if (this.MapHeader.Version == EMapFormat.AB)
            {
                //we can read one spell
                byte buff = reader.ReadByte();
                if (buff != 254)
                {
                    hero.Data.Spells = new List <ESpellId>();
                    hero.Data.Spells.Add(ESpellId.PRESET);

                    if (buff < 254) //255 means no spells
                    {
                        hero.Data.Spells.Add((ESpellId)buff);
                    }
                }
            }

            if (this.MapHeader.Version > EMapFormat.AB)
            {
                bool hasCustomPrimSkills = reader.ReadBoolean();
                if (hasCustomPrimSkills)
                {
                    hero.Data.PrimarySkills = new List <int>();
                    for (int xx = 0; xx < GameConstants.PRIMARY_SKILLS; ++xx)
                    {
                        hero.Data.PrimarySkills.Add(reader.ReadByte());
                    }
                }
            }

            reader.Skip(16);

            return(hero);
        }