Core/Entities: Phasing rewrite

This commit is contained in:
hondacrx
2018-03-28 11:09:30 -04:00
parent caad52f636
commit fa35d60f60
72 changed files with 2254 additions and 1146 deletions
+299
View File
@@ -0,0 +1,299 @@
/*
* Copyright (C) 2012-2018 CypherCore <http://github.com/CypherCore>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Game.Conditions;
using Game.Entities;
using System.Collections.Concurrent;
using System.Linq;
namespace Game
{
public class PhaseShift
{
public bool AddPhase(uint phaseId, PhaseFlags flags, List<Condition> areaConditions, int references = 1)
{
var phase = new PhaseRef(phaseId, flags, null);
ModifyPhasesReferences(phase, references);
if (areaConditions != null)
phase.AreaConditions = areaConditions;
Phases.Add(phase);
return true;
}
public bool RemovePhase(uint phaseId)
{
var phaseRef = new PhaseRef(phaseId, PhaseFlags.None, null);
if (Phases.Contains(phaseRef))
{
ModifyPhasesReferences(phaseRef, -1);
if (phaseRef.References == 0)
{
Phases.Remove(phaseRef);
return true;
}
return false;
}
return false;
}
public bool AddVisibleMapId(uint visibleMapId, TerrainSwapInfo visibleMapInfo, int references = 1)
{
VisibleMapIds.Add(visibleMapId, new VisibleMapIdRef(references, visibleMapInfo));
return true; //Tryadd? maybe Concurrent
}
public bool RemoveVisibleMapId(uint visibleMapId)
{
if (VisibleMapIds.ContainsKey(visibleMapId))
{
var mapIdRef = VisibleMapIds[visibleMapId];
if ((--mapIdRef.References) == 0)
{
VisibleMapIds.Remove(visibleMapId);
return true;
}
return false;
}
return false;
}
public bool AddUiWorldMapAreaIdSwap(uint uiWorldMapAreaId, int references = 1)
{
UiWorldMapAreaIdSwaps.Add(uiWorldMapAreaId, new UiWorldMapAreaIdSwapRef(references));
return true; //Tryadd? maybe Concurrent
}
public bool RemoveUiWorldMapAreaIdSwap(uint uiWorldMapAreaId)
{
if (UiWorldMapAreaIdSwaps.ContainsKey(uiWorldMapAreaId))
{
var value = UiWorldMapAreaIdSwaps[uiWorldMapAreaId];
if ((--value.References) == 0)
{
UiWorldMapAreaIdSwaps.Remove(uiWorldMapAreaId);
return true;
}
return false;
}
return false;
}
public void Clear()
{
ClearPhases();
PersonalGuid.Clear();
VisibleMapIds.Clear();
UiWorldMapAreaIdSwaps.Clear();
}
public void ClearPhases()
{
Flags &= PhaseShiftFlags.AlwaysVisible | PhaseShiftFlags.Inverse;
Phases.Clear();
NonCosmeticReferences = 0;
CosmeticReferences = 0;
DefaultReferences = 0;
UpdateUnphasedFlag();
}
public bool CanSee(PhaseShift other)
{
if (Flags.HasFlag(PhaseShiftFlags.Unphased) && other.Flags.HasFlag(PhaseShiftFlags.Unphased))
return true;
if (Flags.HasFlag(PhaseShiftFlags.AlwaysVisible) || other.Flags.HasFlag(PhaseShiftFlags.AlwaysVisible))
return true;
if (Flags.HasFlag(PhaseShiftFlags.Inverse) && other.Flags.HasFlag(PhaseShiftFlags.Inverse))
return true;
PhaseFlags excludePhasesWithFlag = PhaseFlags.None;
if (Flags.HasFlag(PhaseShiftFlags.NoCosmetic) && other.Flags.HasFlag(PhaseShiftFlags.NoCosmetic))
excludePhasesWithFlag = PhaseFlags.Cosmetic;
if (!Flags.HasFlag(PhaseShiftFlags.Inverse) && !other.Flags.HasFlag(PhaseShiftFlags.Inverse))
{
ObjectGuid ownerGuid = PersonalGuid;
ObjectGuid otherPersonalGuid = other.PersonalGuid;
return Phases.Intersect(other.Phases, (myPhase, otherPhase) =>
{
return !myPhase.Flags.HasAnyFlag(excludePhasesWithFlag) && (!myPhase.Flags.HasFlag(PhaseFlags.Personal) || ownerGuid == otherPersonalGuid);
}).Any();
}
var checkInversePhaseShift = new Func<PhaseShift, PhaseShift, bool>((phaseShift, excludedPhaseShift) =>
{
if (phaseShift.Flags.HasFlag(PhaseShiftFlags.Unphased) && !excludedPhaseShift.Flags.HasFlag(PhaseShiftFlags.InverseUnphased))
return true;
foreach (var itr in phaseShift.Phases)
{
if (itr.Flags.HasAnyFlag(excludePhasesWithFlag))
continue;
var index = excludedPhaseShift.Phases.IndexOf(itr);
if (index == -1 || excludedPhaseShift.Phases[index].Flags.HasAnyFlag(excludePhasesWithFlag))
return true;
}
return false;
});
if (other.Flags.HasFlag(PhaseShiftFlags.Inverse))
return checkInversePhaseShift(this, other);
return checkInversePhaseShift(other, this);
}
public void ModifyPhasesReferences(PhaseRef phaseRef, int references)
{
phaseRef.References += references;
if (!IsDbPhaseShift)
{
if (phaseRef.Flags.HasAnyFlag(PhaseFlags.Cosmetic))
CosmeticReferences += references;
else if (phaseRef.Id != 169)
NonCosmeticReferences += references;
else
DefaultReferences += references;
if (CosmeticReferences != 0)
Flags |= PhaseShiftFlags.NoCosmetic;
else
Flags &= ~PhaseShiftFlags.NoCosmetic;
UpdateUnphasedFlag();
}
}
public void UpdateUnphasedFlag()
{
PhaseShiftFlags unphasedFlag = !Flags.HasAnyFlag(PhaseShiftFlags.Inverse) ? PhaseShiftFlags.Unphased : PhaseShiftFlags.InverseUnphased;
Flags &= ~(!Flags.HasFlag(PhaseShiftFlags.Inverse) ? PhaseShiftFlags.InverseUnphased : PhaseShiftFlags.Unphased);
if (NonCosmeticReferences != 0 && DefaultReferences == 0)
Flags &= ~unphasedFlag;
else
Flags |= unphasedFlag;
}
public bool HasPhase(uint phaseId) { return Phases.Contains(new PhaseRef(phaseId, PhaseFlags.None, null)); }
public List<PhaseRef> GetPhases() { return Phases; }
public bool HasVisibleMapId(uint visibleMapId) { return VisibleMapIds.ContainsKey(visibleMapId); }
public Dictionary<uint, VisibleMapIdRef> GetVisibleMapIds() { return VisibleMapIds; }
public bool HasUiWorldMapAreaIdSwap(uint uiWorldMapAreaId) { return UiWorldMapAreaIdSwaps.ContainsKey(uiWorldMapAreaId); }
public Dictionary<uint, UiWorldMapAreaIdSwapRef> GetUiWorldMapAreaIdSwaps() { return UiWorldMapAreaIdSwaps; }
public PhaseShiftFlags Flags = PhaseShiftFlags.Unphased;
public ObjectGuid PersonalGuid;
public List<PhaseRef> Phases = new List<PhaseRef>();
public Dictionary<uint, VisibleMapIdRef> VisibleMapIds = new Dictionary<uint, VisibleMapIdRef>();
public Dictionary<uint, UiWorldMapAreaIdSwapRef> UiWorldMapAreaIdSwaps = new Dictionary<uint, UiWorldMapAreaIdSwapRef>();
int NonCosmeticReferences;
int CosmeticReferences;
int DefaultReferences;
public bool IsDbPhaseShift;
}
public struct PhaseRef
{
public PhaseRef(uint id, PhaseFlags flags, List<Condition> conditions)
{
Id = id;
Flags = flags;
References = 0;
AreaConditions = conditions;
}
public uint Id;
public PhaseFlags Flags;
public int References;
public List<Condition> AreaConditions;
public static bool operator <(PhaseRef left, PhaseRef right)
{
return left.Id < right.Id;
}
public static bool operator >(PhaseRef left, PhaseRef right)
{
return left.Id > right.Id;
}
public static bool operator ==(PhaseRef left, PhaseRef right) { return left.Id == right.Id; }
public static bool operator !=(PhaseRef left, PhaseRef right) { return !(left == right); }
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
public struct VisibleMapIdRef
{
public VisibleMapIdRef(int references, TerrainSwapInfo visibleMapInfo)
{
References = references;
VisibleMapInfo = visibleMapInfo;
}
public int References;
public TerrainSwapInfo VisibleMapInfo;
}
public struct UiWorldMapAreaIdSwapRef
{
public UiWorldMapAreaIdSwapRef(int references)
{
References = references;
}
public int References;
}
public enum PhaseShiftFlags
{
None = 0x00,
AlwaysVisible = 0x01, // Ignores all phasing, can see everything and be seen by everything
Inverse = 0x02, // By default having at least one shared phase for two objects means they can see each other
// this flag makes objects see each other if they have at least one non-shared phase
InverseUnphased = 0x04,
Unphased = 0x08,
NoCosmetic = 0x10 // This flag ignores shared cosmetic phases (two players that both have shared cosmetic phase but no other phase cannot see each other)
}
public enum PhaseFlags : ushort
{
None = 0x0,
Cosmetic = 0x1,
Personal = 0x2
}
}
+573
View File
@@ -0,0 +1,573 @@
using System;
using System.Collections.Generic;
using System.Text;
using Game.DataStorage;
using Game.Entities;
using Framework.Constants;
using Game.Conditions;
using Game.Spells;
using System.Linq;
using Game.Network.Packets;
using Game.Chat;
using Game.Maps;
namespace Game
{
public class PhasingHandler
{
public static PhaseShift EmptyPhaseShift = new PhaseShift();
public static PhaseFlags GetPhaseFlags(uint phaseId)
{
PhaseRecord phase = CliDB.PhaseStorage.LookupByKey(phaseId);
if (phase != null)
{
if (phase.Flags.HasAnyFlag(PhaseEntryFlags.Cosmetic))
return PhaseFlags.Cosmetic;
if (phase.Flags.HasAnyFlag(PhaseEntryFlags.Personal))
return PhaseFlags.Personal;
}
return PhaseFlags.None;
}
public static void ForAllControlled(Unit unit, Action<Unit> func)
{
foreach (Unit controlled in unit.m_Controlled)
if (controlled.GetTypeId() != TypeId.Player)
func(controlled);
for (byte i = 0; i < SharedConst.MaxSummonSlot; ++i)
{
if (!unit.m_SummonSlot[i].IsEmpty())
{
Creature summon = unit.GetMap().GetCreature(unit.m_SummonSlot[i]);
if (summon)
func(summon);
}
}
}
public static void AddPhase(WorldObject obj, uint phaseId, bool updateVisibility)
{
bool changed = obj.GetPhaseShift().AddPhase(phaseId, GetPhaseFlags(phaseId), null);
Unit unit = obj.ToUnit();
if (unit)
{
unit.OnPhaseChange();
ForAllControlled(unit, controlled =>
{
AddPhase(controlled, phaseId, updateVisibility);
});
unit.RemoveNotOwnSingleTargetAuras(true);
}
UpdateVisibilityIfNeeded(obj, updateVisibility, changed);
}
public static void RemovePhase(WorldObject obj, uint phaseId, bool updateVisibility)
{
bool changed = obj.GetPhaseShift().RemovePhase(phaseId);
Unit unit = obj.ToUnit();
if (unit)
{
unit.OnPhaseChange();
ForAllControlled(unit, controlled =>
{
RemovePhase(controlled, phaseId, updateVisibility);
});
unit.RemoveNotOwnSingleTargetAuras(true);
}
UpdateVisibilityIfNeeded(obj, updateVisibility, changed);
}
public static void AddPhaseGroup(WorldObject obj, uint phaseGroupId, bool updateVisibility)
{
var phasesInGroup = Global.DB2Mgr.GetPhasesForGroup(phaseGroupId);
if (phasesInGroup.Empty())
return;
bool changed = false;
foreach (uint phaseId in phasesInGroup)
changed = obj.GetPhaseShift().AddPhase(phaseId, GetPhaseFlags(phaseId), null) || changed;
Unit unit = obj.ToUnit();
if (unit)
{
unit.OnPhaseChange();
ForAllControlled(unit, controlled =>
{
AddPhaseGroup(controlled, phaseGroupId, updateVisibility);
});
unit.RemoveNotOwnSingleTargetAuras(true);
}
UpdateVisibilityIfNeeded(obj, updateVisibility, changed);
}
public static void RemovePhaseGroup(WorldObject obj, uint phaseGroupId, bool updateVisibility)
{
var phasesInGroup = Global.DB2Mgr.GetPhasesForGroup(phaseGroupId);
if (phasesInGroup.Empty())
return;
bool changed = false;
foreach (uint phaseId in phasesInGroup)
changed = obj.GetPhaseShift().RemovePhase(phaseId) || changed;
Unit unit = obj.ToUnit();
if (unit)
{
unit.OnPhaseChange();
ForAllControlled(unit, controlled =>
{
RemovePhaseGroup(controlled, phaseGroupId, updateVisibility);
});
unit.RemoveNotOwnSingleTargetAuras(true);
}
UpdateVisibilityIfNeeded(obj, updateVisibility, changed);
}
public static void AddVisibleMapId(WorldObject obj, uint visibleMapId)
{
TerrainSwapInfo terrainSwapInfo = Global.ObjectMgr.GetTerrainSwapInfo(visibleMapId);
bool changed = obj.GetPhaseShift().AddVisibleMapId(visibleMapId, terrainSwapInfo);
foreach (uint uiWorldMapAreaIDSwap in terrainSwapInfo.UiWorldMapAreaIDSwaps)
changed = obj.GetPhaseShift().AddUiWorldMapAreaIdSwap(uiWorldMapAreaIDSwap) || changed;
Unit unit = obj.ToUnit();
if (unit)
{
ForAllControlled(unit, controlled =>
{
AddVisibleMapId(controlled, visibleMapId);
});
}
UpdateVisibilityIfNeeded(obj, false, changed);
}
public static void RemoveVisibleMapId(WorldObject obj, uint visibleMapId)
{
TerrainSwapInfo terrainSwapInfo = Global.ObjectMgr.GetTerrainSwapInfo(visibleMapId);
bool changed = obj.GetPhaseShift().RemoveVisibleMapId(visibleMapId);
foreach (uint uiWorldMapAreaIDSwap in terrainSwapInfo.UiWorldMapAreaIDSwaps)
changed = obj.GetPhaseShift().RemoveUiWorldMapAreaIdSwap(uiWorldMapAreaIDSwap) || changed;
Unit unit = obj.ToUnit();
if (unit)
{
ForAllControlled(unit, controlled =>
{
RemoveVisibleMapId(controlled, visibleMapId);
});
}
UpdateVisibilityIfNeeded(obj, false, changed);
}
public static void ResetPhaseShift(WorldObject obj)
{
obj.GetPhaseShift().Clear();
obj.GetSuppressedPhaseShift().Clear();
}
public static void InheritPhaseShift(WorldObject target, WorldObject source)
{
target.SetPhaseShift(source.GetPhaseShift());
target.SetSuppressedPhaseShift(source.GetSuppressedPhaseShift());
}
public static void OnMapChange(WorldObject obj)
{
PhaseShift phaseShift = obj.GetPhaseShift();
PhaseShift suppressedPhaseShift = obj.GetSuppressedPhaseShift();
ConditionSourceInfo srcInfo = new ConditionSourceInfo(obj);
obj.GetPhaseShift().VisibleMapIds.Clear();
obj.GetPhaseShift().UiWorldMapAreaIdSwaps.Clear();
obj.GetSuppressedPhaseShift().VisibleMapIds.Clear();
var visibleMapIds = Global.ObjectMgr.GetTerrainSwapsForMap(obj.GetMapId());
if (!visibleMapIds.Empty())
{
foreach (TerrainSwapInfo visibleMapInfo in visibleMapIds)
{
if (Global.ConditionMgr.IsObjectMeetingNotGroupedConditions(ConditionSourceType.TerrainSwap, visibleMapInfo.Id, srcInfo))
{
phaseShift.AddVisibleMapId(visibleMapInfo.Id, visibleMapInfo);
foreach (uint uiWorldMapAreaIdSwap in visibleMapInfo.UiWorldMapAreaIDSwaps)
phaseShift.AddUiWorldMapAreaIdSwap(uiWorldMapAreaIdSwap);
}
else
suppressedPhaseShift.AddVisibleMapId(visibleMapInfo.Id, visibleMapInfo);
}
}
UpdateVisibilityIfNeeded(obj, false, true);
}
public static void OnAreaChange(WorldObject obj)
{
PhaseShift phaseShift = obj.GetPhaseShift();
PhaseShift suppressedPhaseShift = obj.GetSuppressedPhaseShift();
var oldPhases = phaseShift.GetPhases(); // for comparison
ConditionSourceInfo srcInfo = new ConditionSourceInfo(obj);
obj.GetPhaseShift().ClearPhases();
obj.GetSuppressedPhaseShift().ClearPhases();
uint areaId = obj.GetAreaId();
AreaTableRecord areaEntry = CliDB.AreaTableStorage.LookupByKey(areaId);
while (areaEntry != null)
{
var newAreaPhases = Global.ObjectMgr.GetPhasesForArea(areaEntry.Id);
if (!newAreaPhases.Empty())
{
foreach (PhaseAreaInfo phaseArea in newAreaPhases)
{
if (phaseArea.SubAreaExclusions.Contains(areaId))
continue;
uint phaseId = phaseArea.PhaseInfo.Id;
if (Global.ConditionMgr.IsObjectMeetToConditions(srcInfo, phaseArea.Conditions))
phaseShift.AddPhase(phaseId, GetPhaseFlags(phaseId), phaseArea.Conditions);
else
suppressedPhaseShift.AddPhase(phaseId, GetPhaseFlags(phaseId), phaseArea.Conditions);
}
}
areaEntry = CliDB.AreaTableStorage.LookupByKey(areaEntry.ParentAreaID);
}
bool changed = phaseShift.GetPhases() != oldPhases;
Unit unit = obj.ToUnit();
if (unit)
{
foreach (AuraEffect aurEff in unit.GetAuraEffectsByType(AuraType.Phase))
{
uint phaseId = (uint)aurEff.GetMiscValueB();
changed = phaseShift.AddPhase(phaseId, GetPhaseFlags(phaseId), null) || changed;
}
foreach (AuraEffect aurEff in unit.GetAuraEffectsByType(AuraType.PhaseGroup))
{
var phasesInGroup = Global.DB2Mgr.GetPhasesForGroup((uint)aurEff.GetMiscValueB());
foreach (uint phaseId in phasesInGroup)
changed = phaseShift.AddPhase(phaseId, GetPhaseFlags(phaseId), null) || changed;
}
if (changed)
unit.OnPhaseChange();
ForAllControlled(unit, controlled =>
{
InheritPhaseShift(controlled, unit);
});
if (changed)
unit.RemoveNotOwnSingleTargetAuras(true);
}
UpdateVisibilityIfNeeded(obj, true, changed);
}
public static void OnConditionChange(WorldObject obj)
{
PhaseShift phaseShift = obj.GetPhaseShift();
PhaseShift suppressedPhaseShift = obj.GetSuppressedPhaseShift();
PhaseShift newSuppressions = new PhaseShift();
ConditionSourceInfo srcInfo = new ConditionSourceInfo(obj);
bool changed = false;
foreach (var phaseRef in phaseShift.Phases.ToArray())
{
if (!phaseRef.AreaConditions.Empty() && !Global.ConditionMgr.IsObjectMeetToConditions(srcInfo, phaseRef.AreaConditions))
{
newSuppressions.AddPhase(phaseRef.Id, phaseRef.Flags, phaseRef.AreaConditions, phaseRef.References);
phaseShift.Phases.Remove(phaseRef);
}
}
foreach (var phaseRef in suppressedPhaseShift.Phases.ToArray())
{
if (Global.ConditionMgr.IsObjectMeetToConditions(srcInfo, phaseRef.AreaConditions))
{
changed = phaseShift.AddPhase(phaseRef.Id, phaseRef.Flags, phaseRef.AreaConditions, phaseRef.References) || changed;
suppressedPhaseShift.Phases.Remove(phaseRef);
}
}
foreach (var pair in phaseShift.VisibleMapIds.ToList())
{
if (!Global.ConditionMgr.IsObjectMeetingNotGroupedConditions(ConditionSourceType.TerrainSwap, pair.Key, srcInfo))
{
newSuppressions.AddVisibleMapId(pair.Key, pair.Value.VisibleMapInfo, pair.Value.References);
foreach (uint uiWorldMapAreaIdSwap in pair.Value.VisibleMapInfo.UiWorldMapAreaIDSwaps)
changed = phaseShift.RemoveUiWorldMapAreaIdSwap(uiWorldMapAreaIdSwap) || changed;
phaseShift.VisibleMapIds.Remove(pair.Key);
}
}
foreach (var pair in suppressedPhaseShift.VisibleMapIds.ToList())
{
if (Global.ConditionMgr.IsObjectMeetingNotGroupedConditions(ConditionSourceType.TerrainSwap, pair.Key, srcInfo))
{
changed = phaseShift.AddVisibleMapId(pair.Key, pair.Value.VisibleMapInfo, pair.Value.References) || changed;
foreach (uint uiWorldMapAreaIdSwap in pair.Value.VisibleMapInfo.UiWorldMapAreaIDSwaps)
changed = phaseShift.AddUiWorldMapAreaIdSwap(uiWorldMapAreaIdSwap) || changed;
suppressedPhaseShift.VisibleMapIds.Remove(pair.Key);
}
}
Unit unit = obj.ToUnit();
if (unit)
{
foreach (AuraEffect aurEff in unit.GetAuraEffectsByType(AuraType.Phase))
{
uint phaseId = (uint)aurEff.GetMiscValueB();
var eraseResult = newSuppressions.RemovePhase(phaseId);
// if condition was met previously there is nothing to erase
if (newSuppressions.RemovePhase(phaseId))
phaseShift.AddPhase(phaseId, GetPhaseFlags(phaseId), null);//todo needs checked
}
foreach (AuraEffect aurEff in unit.GetAuraEffectsByType(AuraType.PhaseGroup))
{
var phasesInGroup = Global.DB2Mgr.GetPhasesForGroup((uint)aurEff.GetMiscValueB());
if (!phasesInGroup.Empty())
{
foreach (uint phaseId in phasesInGroup)
{
var eraseResult = newSuppressions.RemovePhase(phaseId);
// if condition was met previously there is nothing to erase
if (eraseResult)
phaseShift.AddPhase(phaseId, GetPhaseFlags(phaseId), null);
}
}
}
}
changed = changed || !newSuppressions.Phases.Empty() || !newSuppressions.VisibleMapIds.Empty();
foreach (var phaseRef in newSuppressions.Phases)
suppressedPhaseShift.AddPhase(phaseRef.Id, phaseRef.Flags, phaseRef.AreaConditions, phaseRef.References);
foreach (var pair in newSuppressions.VisibleMapIds)
suppressedPhaseShift.AddVisibleMapId(pair.Key, pair.Value.VisibleMapInfo, pair.Value.References);
if (unit)
{
if (changed)
unit.OnPhaseChange();
ForAllControlled(unit, controlled =>
{
InheritPhaseShift(controlled, unit);
});
if (changed)
unit.RemoveNotOwnSingleTargetAuras(true);
}
UpdateVisibilityIfNeeded(obj, true, changed);
}
public static void SendToPlayer(Player player, PhaseShift phaseShift)
{
PhaseShiftChange phaseShiftChange = new PhaseShiftChange();
phaseShiftChange.Client = player.GetGUID();
phaseShiftChange.Phaseshift.PhaseShiftFlags = (uint)phaseShift.Flags;
phaseShiftChange.Phaseshift.PersonalGUID = phaseShift.PersonalGuid;
foreach (var phaseRef in phaseShift.Phases)
phaseShiftChange.Phaseshift.Phases.Add(new PhaseShiftDataPhase((uint)phaseRef.Flags, phaseRef.Id));
foreach (var visibleMapId in phaseShift.VisibleMapIds)
phaseShiftChange.VisibleMapIDs.Add((ushort)visibleMapId.Key);
foreach (var uiWorldMapAreaIdSwap in phaseShift.UiWorldMapAreaIdSwaps)
phaseShiftChange.UiWorldMapAreaIDSwaps.Add((ushort)uiWorldMapAreaIdSwap.Key);
player.SendPacket(phaseShiftChange);
}
public static void SendToPlayer(Player player)
{
SendToPlayer(player, player.GetPhaseShift());
}
public static void FillPartyMemberPhase(PartyMemberPhaseStates partyMemberPhases, PhaseShift phaseShift)
{
partyMemberPhases.PhaseShiftFlags = (int)phaseShift.Flags;
partyMemberPhases.PersonalGUID = phaseShift.PersonalGuid;
foreach (var phase in phaseShift.Phases)
partyMemberPhases.List.Add(new PartyMemberPhase((uint)phase.Flags, phase.Id));
}
public static void InitDbPhaseShift(PhaseShift phaseShift, PhaseUseFlagsValues phaseUseFlags, uint phaseId, uint phaseGroupId)
{
phaseShift.ClearPhases();
phaseShift.IsDbPhaseShift = true;
PhaseShiftFlags flags = PhaseShiftFlags.None;
if (phaseUseFlags.HasAnyFlag(PhaseUseFlagsValues.AlwaysVisible))
flags = flags | PhaseShiftFlags.AlwaysVisible | PhaseShiftFlags.Unphased;
if (phaseUseFlags.HasAnyFlag(PhaseUseFlagsValues.Inverse))
flags |= PhaseShiftFlags.Inverse;
if (phaseId != 0)
phaseShift.AddPhase(phaseId, GetPhaseFlags(phaseId), null);
else
{
var phasesInGroup = Global.DB2Mgr.GetPhasesForGroup(phaseGroupId);
foreach (uint phaseInGroup in phasesInGroup)
phaseShift.AddPhase(phaseInGroup, GetPhaseFlags(phaseInGroup), null);
}
if (phaseShift.Phases.Empty() || phaseShift.HasPhase(169))
{
if (flags.HasFlag(PhaseShiftFlags.Inverse))
flags |= PhaseShiftFlags.InverseUnphased;
else
flags |= PhaseShiftFlags.Unphased;
}
phaseShift.Flags = flags;
}
public static void InitDbVisibleMapId(PhaseShift phaseShift, int visibleMapId)
{
phaseShift.VisibleMapIds.Clear();
if (visibleMapId != -1)
phaseShift.AddVisibleMapId((uint)visibleMapId, Global.ObjectMgr.GetTerrainSwapInfo((uint)visibleMapId));
}
public static bool InDbPhaseShift(WorldObject obj, PhaseUseFlagsValues phaseUseFlags, ushort phaseId, uint phaseGroupId)
{
PhaseShift phaseShift = new PhaseShift();
InitDbPhaseShift(phaseShift, phaseUseFlags, phaseId, phaseGroupId);
return obj.GetPhaseShift().CanSee(phaseShift);
}
public static uint GetTerrainMapId(PhaseShift phaseShift, Map map, float x, float y)
{
if (phaseShift.VisibleMapIds.Empty())
return map.GetId();
if (phaseShift.VisibleMapIds.Count == 1)
return phaseShift.VisibleMapIds.First().Key;
GridCoord gridCoord = GridDefines.ComputeGridCoord(x, y);
uint gx = (uint)((MapConst.MaxGrids - 1) - gridCoord.x_coord);
uint gy = (uint)((MapConst.MaxGrids - 1) - gridCoord.y_coord);
uint gxbegin = Math.Max(gx - 1, 0);
uint gxend = Math.Min(gx + 1, MapConst.MaxGrids);
uint gybegin = Math.Max(gy - 1, 0);
uint gyend = Math.Min(gy + 1, MapConst.MaxGrids);
foreach (var itr in phaseShift.VisibleMapIds)
for (uint gxi = gxbegin; gxi < gxend; ++gxi)
for (uint gyi = gybegin; gyi < gyend; ++gyi)
if (map.HasGridMap(itr.Key, gxi, gyi))
return itr.Key;
return map.GetId();
}
public static void SetAlwaysVisible(PhaseShift phaseShift, bool apply)
{
if (apply)
phaseShift.Flags |= PhaseShiftFlags.AlwaysVisible;
else
phaseShift.Flags &= ~PhaseShiftFlags.AlwaysVisible;
}
public static void SetInversed(PhaseShift phaseShift, bool apply)
{
if (apply)
phaseShift.Flags |= PhaseShiftFlags.Inverse;
else
phaseShift.Flags &= ~PhaseShiftFlags.Inverse;
phaseShift.UpdateUnphasedFlag();
}
public static void PrintToChat(CommandHandler chat, PhaseShift phaseShift)
{
chat.SendSysMessage(CypherStrings.PhaseshiftStatus, phaseShift.Flags, phaseShift.PersonalGuid.ToString());
if (!phaseShift.Phases.Empty())
{
StringBuilder phases = new StringBuilder();
string cosmetic = Global.ObjectMgr.GetCypherString(CypherStrings.PhaseFlagCosmetic, chat.GetSessionDbcLocale());
string personal = Global.ObjectMgr.GetCypherString(CypherStrings.PhaseFlagPersonal, chat.GetSessionDbcLocale());
foreach (PhaseRef phase in phaseShift.Phases)
{
phases.Append(phase.Id);
if (phase.Flags.HasFlag(PhaseFlags.Cosmetic))
phases.Append(' ' + '(' + cosmetic + ')');
if (phase.Flags.HasFlag(PhaseFlags.Personal))
phases.Append(' ' + '(' + personal + ')');
phases.Append(", ");
}
chat.SendSysMessage(CypherStrings.PhaseshiftPhases, phases.ToString());
}
if (!phaseShift.VisibleMapIds.Empty())
{
StringBuilder visibleMapIds = new StringBuilder();
foreach (var visibleMapId in phaseShift.VisibleMapIds)
visibleMapIds.Append(visibleMapId.Key + ',' + ' ');
chat.SendSysMessage(CypherStrings.PhaseshiftVisibleMapIds, visibleMapIds.ToString());
}
if (!phaseShift.UiWorldMapAreaIdSwaps.Empty())
{
StringBuilder uiWorldMapAreaIdSwaps = new StringBuilder();
foreach (var uiWorldMapAreaIdSwap in phaseShift.UiWorldMapAreaIdSwaps)
uiWorldMapAreaIdSwaps.Append(uiWorldMapAreaIdSwap.Key + ',' + ' ');
chat.SendSysMessage(CypherStrings.PhaseshiftUiWorldMapAreaSwaps, uiWorldMapAreaIdSwaps.ToString());
}
}
public static string FormatPhases(PhaseShift phaseShift)
{
StringBuilder phases = new StringBuilder();
foreach (var phase in phaseShift.Phases)
phases.Append(phase.Id + ',');
return phases.ToString();
}
static void UpdateVisibilityIfNeeded(WorldObject obj, bool updateVisibility, bool changed)
{
if (changed && obj.IsInWorld)
{
Player player = obj.ToPlayer();
if (player)
SendToPlayer(player);
if (updateVisibility)
{
if (player)
player.GetMap().SendUpdateTransportVisibility(player);
obj.UpdateObjectVisibility();
}
}
}
}
}