Example #1
0
        /// <summary>
        /// Is called to initialize the object; usually after a set of other operations have been performed or if
        /// the right time has come and other required steps have been performed.
        /// </summary>
        public void FinalizeDataHolder()
        {
            if (string.IsNullOrEmpty(DefaultName))
            {
                ContentMgr.OnInvalidDBData("NPCEntry has no name: " + this);
                return;
            }

            if (Titles == null)
            {
                Titles = new string[(int)ClientLocale.End];
            }

            if (DefaultTitle == null)
            {
                DefaultTitle = "";
            }

            if (SpellGroupId != 0 && NPCMgr.PetSpells != null)
            {
                var spells = NPCMgr.PetSpells.Get(SpellGroupId) ?? Spell.EmptyArray;
                foreach (var spell in spells)
                {
                    AddSpell(spell);
                }
            }

            if (MinMana > MaxMana)
            {
                MaxMana = MinMana;
            }

            if (MaxDamage > 0)
            {
                if (MinDamage < 1)
                {
                    MinDamage = 1;
                }
                if (MaxDamage < MinDamage)
                {
                    MaxDamage = MinDamage;
                }
            }

            if (RangedMaxDamage > 0 && RangedMinDamage > 0)
            {
                if (RangedMaxDamage < RangedMinDamage)
                {
                    RangedMaxDamage = RangedMinDamage;
                }
                if (RangedAttackTime == 0)
                {
                    RangedAttackTime = AttackTime;
                }
            }

            AggroBaseRange = AggroBaseRangeDefault;

            NPCId = (NPCId)Id;

            DefaultDecayDelayMillis = _DefaultDecayDelayMillis;
            Family = NPCMgr.GetFamily(FamilyId);

            if (Type == CreatureType.NotSpecified || VehicleEntry != null)
            {
                IsIdle = true;
            }

            if (Type == CreatureType.NotSpecified && UnitFlags.HasFlag((UnitFlags.Passive | UnitFlags.NotSelectable)))
            {
                IsEventTrigger = true;
                IsIdle         = false;
            }

            if (Resistances == null)
            {
                Resistances = new int[ItemConstants.MaxResCount];
            }

            SetFlagIndices = Utility.GetSetIndices((uint)NPCFlags);

            // set/fix factions
            HordeFaction    = FactionMgr.Get(HordeFactionId);
            AllianceFaction = FactionMgr.Get(AllianceFactionId);
            if (HordeFaction == null)
            {
                HordeFaction   = AllianceFaction;
                HordeFactionId = AllianceFactionId;
            }
            else if (AllianceFaction == null)
            {
                AllianceFaction   = HordeFaction;
                AllianceFactionId = HordeFactionId;
            }
            if (AllianceFaction == null)
            {
                ContentMgr.OnInvalidDBData("NPCEntry has no valid Faction: " + this);
                HordeFaction   = AllianceFaction = NPCMgr.DefaultFaction;
                HordeFactionId = AllianceFactionId = (FactionTemplateId)HordeFaction.Template.Id;
            }

            // speeds
            if (SpeedFactor < 0.01)
            {
                SpeedFactor = 1;
            }

            if (WalkSpeed == 0)
            {
                WalkSpeed = NPCMgr.DefaultNPCWalkSpeed * SpeedFactor;
            }
            if (RunSpeed == 0)
            {
                RunSpeed = NPCMgr.DefaultNPCRunSpeed * SpeedFactor;
            }
            if (FlySpeed == 0)
            {
                FlySpeed = NPCMgr.DefaultNPCFlySpeed * SpeedFactor;
            }

            // Add all default spells
            if (FixedSpells != null)
            {
                foreach (var spell in FixedSpells)
                {
                    AddSpell(spell);
                }
            }

            InstanceTypeHandlers = NPCMgr.GetNPCTypeHandlers(this);
            SpawnTypeHandlers    = NPCMgr.GetNPCSpawnTypeHandlers(this);

            //ArrayUtil.PruneVals(ref DisplayIds);

            if (EquipmentId != 0)
            {
                Equipment = NPCMgr.GetEquipment(EquipmentId);
            }

            ModelInfos = new UnitModelInfo[DisplayIds.Length];

            GeneratesXp = (Type != CreatureType.Critter && Type != CreatureType.None && !ExtraFlags.HasFlag(UnitExtraFlags.NoXP));

            var x = 0;

            for (var i = 0; i < DisplayIds.Length; i++)
            {
                var did = DisplayIds[i];
                if (did > 0)
                {
                    var model = UnitMgr.GetModelInfo(did);
                    if (model != null)
                    {
                        ModelInfos[x++] = model;
                    }
                }
            }

            if (x == 0)
            {
                ContentMgr.OnInvalidDBData("NPCEntry has no valid DisplayId: {0} ({1})", this, DisplayIds.ToString(", "));
                return;
            }

            if (TrainerTemplateId != 0)
            {
                if (!NPCMgr.TrainerSpellTemplates.ContainsKey(TrainerTemplateId))
                {
                    ContentMgr.OnInvalidDBData("NPCEntry has invalid TrainerTemplateId: {0} ({1})", this, TrainerTemplateId);
                }
                else
                {
                    if (TrainerEntry == null)
                    {
                        TrainerEntry = new TrainerEntry();
                    }
                    foreach (var trainerSpell in NPCMgr.TrainerSpellTemplates[TrainerTemplateId])
                    {
                        TrainerEntry.AddSpell(trainerSpell);
                    }
                }
            }

            if (AddonData != null)
            {
                AddonData.InitAddonData(this);
            }

            if (x < ModelInfos.Length)
            {
                Array.Resize(ref ModelInfos, x);
            }

            // add to container
            if (Id < NPCMgr.Entries.Length)
            {
                NPCMgr.Entries[Id] = this;
            }
            else
            {
                NPCMgr.CustomEntries[Id] = this;
            }
            ++NPCMgr.EntryCount;

            if (BrainCreator == null)
            {
                BrainCreator = DefaultBrainCreator;
            }

            if (NPCCreator == null)
            {
                NPCCreator = DefaultCreator;
            }
        }