public bool SetCharmedBy(Unit charmer, CharmType type, AuraApplication aurApp = null) { if (!charmer) { return(false); } // dismount players when charmed if (IsTypeId(TypeId.Player)) { RemoveAurasByType(AuraType.Mounted); } if (charmer.IsTypeId(TypeId.Player)) { charmer.RemoveAurasByType(AuraType.Mounted); } Contract.Assert(type != CharmType.Possess || charmer.IsTypeId(TypeId.Player)); Contract.Assert((type == CharmType.Vehicle) == IsVehicle()); Log.outDebug(LogFilter.Unit, "SetCharmedBy: charmer {0} (GUID {1}), charmed {2} (GUID {3}), type {4}.", charmer.GetEntry(), charmer.GetGUID().ToString(), GetEntry(), GetGUID().ToString(), type); if (this == charmer) { Log.outFatal(LogFilter.Unit, "Unit:SetCharmedBy: Unit {0} (GUID {1}) is trying to charm itself!", GetEntry(), GetGUID().ToString()); return(false); } if (IsTypeId(TypeId.Player) && ToPlayer().GetTransport()) { Log.outFatal(LogFilter.Unit, "Unit:SetCharmedBy: Player on transport is trying to charm {0} (GUID {1})", GetEntry(), GetGUID().ToString()); return(false); } // Already charmed if (!GetCharmerGUID().IsEmpty()) { Log.outFatal(LogFilter.Unit, "Unit:SetCharmedBy: {0} (GUID {1}) has already been charmed but {2} (GUID {3}) is trying to charm it!", GetEntry(), GetGUID().ToString(), charmer.GetEntry(), charmer.GetGUID().ToString()); return(false); } CastStop(); CombatStop(); // @todo CombatStop(true) may cause crash (interrupt spells) DeleteThreatList(); Player playerCharmer = charmer.ToPlayer(); // Charmer stop charming if (playerCharmer) { playerCharmer.StopCastingCharm(); playerCharmer.StopCastingBindSight(); } // Charmed stop charming if (IsTypeId(TypeId.Player)) { ToPlayer().StopCastingCharm(); ToPlayer().StopCastingBindSight(); } // StopCastingCharm may remove a possessed pet? if (!IsInWorld) { Log.outFatal(LogFilter.Unit, "Unit:SetCharmedBy: {0} (GUID {1}) is not in world but {2} (GUID {3}) is trying to charm it!", GetEntry(), GetGUID().ToString(), charmer.GetEntry(), charmer.GetGUID().ToString()); return(false); } // charm is set by aura, and aura effect remove handler was called during apply handler execution // prevent undefined behaviour if (aurApp != null && aurApp.GetRemoveMode() != 0) { return(false); } _oldFactionId = getFaction(); SetFaction(charmer.getFaction()); // Set charmed charmer.SetCharm(this, true); Player player; if (IsTypeId(TypeId.Unit)) { ToCreature().GetAI().OnCharmed(true); GetMotionMaster().MoveIdle(); } else if (player = ToPlayer()) { if (player.isAFK()) { player.ToggleAFK(); } Creature creatureCharmer = charmer.ToCreature(); if (charmer.IsTypeId(TypeId.Unit)) // we are charmed by a creature { // change AI to charmed AI on next Update tick NeedChangeAI = true; if (IsAIEnabled) { IsAIEnabled = false; player.GetAI().OnCharmed(true); } } player.SetClientControl(this, false); } // charm is set by aura, and aura effect remove handler was called during apply handler execution // prevent undefined behaviour if (aurApp != null && aurApp.GetRemoveMode() != 0) { return(false); } // Pets already have a properly initialized CharmInfo, don't overwrite it. if (type != CharmType.Vehicle && GetCharmInfo() == null) { InitCharmInfo(); if (type == CharmType.Possess) { GetCharmInfo().InitPossessCreateSpells(); } else { GetCharmInfo().InitCharmCreateSpells(); } } if (playerCharmer) { switch (type) { case CharmType.Vehicle: SetFlag(UnitFields.Flags, UnitFlags.PlayerControlled); playerCharmer.SetClientControl(this, true); playerCharmer.VehicleSpellInitialize(); break; case CharmType.Possess: AddUnitState(UnitState.Possessed); SetFlag(UnitFields.Flags, UnitFlags.PlayerControlled); charmer.SetFlag(UnitFields.Flags, UnitFlags.RemoveClientControl); playerCharmer.SetClientControl(this, true); playerCharmer.PossessSpellInitialize(); break; case CharmType.Charm: if (IsTypeId(TypeId.Unit) && charmer.GetClass() == Class.Warlock) { CreatureTemplate cinfo = ToCreature().GetCreatureTemplate(); if (cinfo != null && cinfo.CreatureType == CreatureType.Demon) { // to prevent client crash SetByteValue(UnitFields.Bytes0, 1, (byte)Class.Mage); // just to enable stat window if (GetCharmInfo() != null) { GetCharmInfo().SetPetNumber(Global.ObjectMgr.GeneratePetNumber(), true); } // if charmed two demons the same session, the 2nd gets the 1st one's name SetUInt32Value(UnitFields.PetNameTimestamp, (uint)Time.UnixTime); // cast can't be helped } } playerCharmer.CharmSpellInitialize(); break; default: case CharmType.Convert: break; } } return(true); }
public bool SetCharmedBy(Unit charmer, CharmType type, AuraApplication aurApp = null) { if (!charmer) { return(false); } // dismount players when charmed if (IsTypeId(TypeId.Player)) { RemoveAurasByType(AuraType.Mounted); } if (charmer.IsTypeId(TypeId.Player)) { charmer.RemoveAurasByType(AuraType.Mounted); } Cypher.Assert(type != CharmType.Possess || charmer.IsTypeId(TypeId.Player)); Cypher.Assert((type == CharmType.Vehicle) == (GetVehicleKit() && GetVehicleKit().IsControllableVehicle())); Log.outDebug(LogFilter.Unit, "SetCharmedBy: charmer {0} (GUID {1}), charmed {2} (GUID {3}), type {4}.", charmer.GetEntry(), charmer.GetGUID().ToString(), GetEntry(), GetGUID().ToString(), type); if (this == charmer) { Log.outFatal(LogFilter.Unit, "Unit:SetCharmedBy: Unit {0} (GUID {1}) is trying to charm itself!", GetEntry(), GetGUID().ToString()); return(false); } if (IsTypeId(TypeId.Player) && ToPlayer().GetTransport()) { Log.outFatal(LogFilter.Unit, "Unit:SetCharmedBy: Player on transport is trying to charm {0} (GUID {1})", GetEntry(), GetGUID().ToString()); return(false); } // Already charmed if (!GetCharmerGUID().IsEmpty()) { Log.outFatal(LogFilter.Unit, "Unit:SetCharmedBy: {0} (GUID {1}) has already been charmed but {2} (GUID {3}) is trying to charm it!", GetEntry(), GetGUID().ToString(), charmer.GetEntry(), charmer.GetGUID().ToString()); return(false); } CastStop(); CombatStop(); // @todo CombatStop(true) may cause crash (interrupt spells) Player playerCharmer = charmer.ToPlayer(); // Charmer stop charming if (playerCharmer) { playerCharmer.StopCastingCharm(); playerCharmer.StopCastingBindSight(); } // Charmed stop charming if (IsTypeId(TypeId.Player)) { ToPlayer().StopCastingCharm(); ToPlayer().StopCastingBindSight(); } // StopCastingCharm may remove a possessed pet? if (!IsInWorld) { Log.outFatal(LogFilter.Unit, "Unit:SetCharmedBy: {0} (GUID {1}) is not in world but {2} (GUID {3}) is trying to charm it!", GetEntry(), GetGUID().ToString(), charmer.GetEntry(), charmer.GetGUID().ToString()); return(false); } // charm is set by aura, and aura effect remove handler was called during apply handler execution // prevent undefined behaviour if (aurApp != null && aurApp.GetRemoveMode() != 0) { return(false); } _oldFactionId = GetFaction(); SetFaction(charmer.GetFaction()); // Pause any Idle movement PauseMovement(0, 0, false); // Remove any active voluntary movement GetMotionMaster().Clear(MovementGeneratorPriority.Normal); // Stop any remaining spline, if no involuntary movement is found Func <MovementGenerator, bool> criteria = movement => movement.Priority == MovementGeneratorPriority.Highest; if (!GetMotionMaster().HasMovementGenerator(criteria)) { StopMoving(); } // Set charmed charmer.SetCharm(this, true); Player player = ToPlayer(); if (player) { if (player.IsAFK()) { player.ToggleAFK(); } player.SetClientControl(this, false); } // charm is set by aura, and aura effect remove handler was called during apply handler execution // prevent undefined behaviour if (aurApp != null && aurApp.GetRemoveMode() != 0) { return(false); } // Pets already have a properly initialized CharmInfo, don't overwrite it. if (type != CharmType.Vehicle && GetCharmInfo() == null) { InitCharmInfo(); if (type == CharmType.Possess) { GetCharmInfo().InitPossessCreateSpells(); } else { GetCharmInfo().InitCharmCreateSpells(); } } if (playerCharmer) { switch (type) { case CharmType.Vehicle: AddUnitFlag(UnitFlags.Possessed); playerCharmer.SetClientControl(this, true); playerCharmer.VehicleSpellInitialize(); break; case CharmType.Possess: AddUnitFlag(UnitFlags.Possessed); charmer.AddUnitFlag(UnitFlags.RemoveClientControl); playerCharmer.SetClientControl(this, true); playerCharmer.PossessSpellInitialize(); AddUnitState(UnitState.Possessed); break; case CharmType.Charm: if (IsTypeId(TypeId.Unit) && charmer.GetClass() == Class.Warlock) { CreatureTemplate cinfo = ToCreature().GetCreatureTemplate(); if (cinfo != null && cinfo.CreatureType == CreatureType.Demon) { // to prevent client crash SetClass(Class.Mage); // just to enable stat window if (GetCharmInfo() != null) { GetCharmInfo().SetPetNumber(Global.ObjectMgr.GeneratePetNumber(), true); } // if charmed two demons the same session, the 2nd gets the 1st one's name SetPetNameTimestamp((uint)GameTime.GetGameTime()); // cast can't be helped } } playerCharmer.CharmSpellInitialize(); break; default: case CharmType.Convert: break; } } AddUnitState(UnitState.Charmed); if (!IsPlayer() || !charmer.IsPlayer()) { // AI will schedule its own change if appropriate UnitAI ai = GetAI(); if (ai != null) { ai.OnCharmed(false); } else { ScheduleAIChange(); } } return(true); }