/Server/WorldServer/World/Map.cs

https://bitbucket.org/arcanosteam/arcanos · C# · 162 lines · 152 code · 10 blank · 0 comment · 26 complexity · 6da780c7b1cb15c20aac4c0ec4bb528b MD5 · raw file

  1. using System.Collections.Generic;
  2. using Arcanos.Server.WorldServer.Spells;
  3. using Arcanos.Shared;
  4. using Arcanos.Shared.Grids;
  5. using Arcanos.Shared.Net;
  6. using Arcanos.Shared.Objects;
  7. using Arcanos.Shared.World;
  8. using Arcanos.Server.WorldServer.Objects;
  9. using Microsoft.Xna.Framework;
  10. namespace Arcanos.Server.WorldServer.World
  11. {
  12. public class Map : MapBase
  13. {
  14. public const float DEFAULT_VISIBILITY_DISTANCE = 100;
  15. public readonly List<PlayerCreature> Players = new List<PlayerCreature>();
  16. public readonly List<AreaEffect> AreaEffects = new List<AreaEffect>();
  17. private bool updatingAreaEffects;
  18. private readonly Queue<AreaEffect> areaEffectsToAdd = new Queue<AreaEffect>();
  19. private readonly Queue<AreaEffect> areaEffectsToRemove = new Queue<AreaEffect>();
  20. public float VisibilityDistance = DEFAULT_VISIBILITY_DISTANCE;
  21. public Map(ulong guid, MapTemplate template) : base(guid, template)
  22. {
  23. Environment = new Environment(template.Environment);
  24. }
  25. public override void Update()
  26. {
  27. base.Update();
  28. foreach (PlayerCreature pc in Players)
  29. {
  30. GridBounds bounds = Grid.GetGridBounds(pc.Position, VisibilityDistance);
  31. for (int x = bounds.MinX; x <= bounds.MaxX; ++x)
  32. for (int y = bounds.MinY; y <= bounds.MaxY; ++y)
  33. Grid.Cells[x][y].MarkAsActive();
  34. }
  35. Grid.UpdateActiveCells();
  36. updatingAreaEffects = true;
  37. foreach (AreaEffect areaEffect in AreaEffects)
  38. areaEffect.Update();
  39. while (areaEffectsToAdd.Count != 0)
  40. AreaEffects.Add(areaEffectsToAdd.Dequeue());
  41. while (areaEffectsToRemove.Count != 0)
  42. AreaEffects.Remove(areaEffectsToRemove.Dequeue());
  43. updatingAreaEffects = false;
  44. }
  45. public override void Add(WorldObjectBase wo)
  46. {
  47. if (wo.Type.IsPlayer())
  48. Players.Add(wo as PlayerCreature);
  49. base.Add(wo);
  50. }
  51. public override void Remove(WorldObjectBase wo)
  52. {
  53. if (wo.Type.IsPlayer())
  54. Players.Remove(wo as PlayerCreature);
  55. base.Remove(wo);
  56. }
  57. public void Add(AreaEffect areaEffect)
  58. {
  59. if (updatingAreaEffects)
  60. areaEffectsToAdd.Enqueue(areaEffect);
  61. else
  62. AreaEffects.Add(areaEffect);
  63. }
  64. public void Remove(AreaEffect areaEffect)
  65. {
  66. if (updatingAreaEffects)
  67. areaEffectsToRemove.Enqueue(areaEffect);
  68. else
  69. AreaEffects.Remove(areaEffect);
  70. }
  71. public override void MoveObject(WorldObjectBase wo)
  72. {
  73. base.MoveObject(wo);
  74. if (wo.Type.IsCreature())
  75. foreach (AreaEffect areaEffect in AreaEffects)
  76. if (IsAffectedByAreaEffect(wo as CreatureBase, areaEffect) && !areaEffect.AffectedTargets.Contains(wo as CreatureBase))
  77. areaEffect.CreatureEnteredArea(wo as CreatureBase);
  78. }
  79. public WorldObject Spawn(ulong guid, WorldObjectTemplate template, Vector3 pos)
  80. {
  81. return Spawn(guid, template, pos, Rotation.Zero);
  82. }
  83. public WorldObject Spawn(ulong guid, WorldObjectTemplate template, Vector3 pos, Rotation rot)
  84. {
  85. WorldObject wo = new WorldObject(guid, template, pos, rot);
  86. Add(wo);
  87. return wo;
  88. }
  89. public Creature Spawn(ulong guid, CreatureTemplate template, Vector3 pos)
  90. {
  91. return Spawn(guid, template, pos, Rotation.Zero);
  92. }
  93. public Creature Spawn(ulong guid, CreatureTemplate template, Vector3 pos, Rotation rot)
  94. {
  95. Creature creature = new Creature(guid, template, pos, rot);
  96. Add(creature);
  97. return creature;
  98. }
  99. public override IEnumerable<WorldObjectBase> GetVisibleObjects(Vector3 pos)
  100. {
  101. return GetNearbyObjects(pos, VisibilityDistance);
  102. }
  103. public IEnumerable<AreaEffect> GetAffectingAreaEffects(CreatureBase creature)
  104. {
  105. foreach (AreaEffect areaEffect in AreaEffects)
  106. if (IsAffectedByAreaEffect(creature, areaEffect))
  107. yield return areaEffect;
  108. }
  109. public bool IsAffectedByAreaEffect(CreatureBase creature, AreaEffect areaEffect)
  110. {
  111. return creature.DistanceTo(areaEffect.Position) <= areaEffect.Spell.Radius;
  112. }
  113. public bool IsAffectedByAreaEffects(CreatureBase creature)
  114. {
  115. foreach (AreaEffect areaEffect in AreaEffects)
  116. if (IsAffectedByAreaEffect(creature, areaEffect))
  117. return true;
  118. return false;
  119. }
  120. public bool IsAffectedByAreaEffectsExcept(CreatureBase creature, AreaEffect exceptAreaEffect)
  121. {
  122. foreach (AreaEffect areaEffect in AreaEffects)
  123. if (areaEffect != exceptAreaEffect && IsAffectedByAreaEffect(creature, areaEffect))
  124. return true;
  125. return false;
  126. }
  127. public bool IsAffectedByAreaEffectsOfSpell(CreatureBase creature, SpellTemplate spell)
  128. {
  129. foreach (AreaEffect areaEffect in AreaEffects)
  130. if (areaEffect.Spell == spell && IsAffectedByAreaEffect(creature, areaEffect))
  131. return true;
  132. return false;
  133. }
  134. public bool IsAffectedByAreaEffectsOfSpellExcept(CreatureBase creature, SpellTemplate spell, AreaEffect exceptAreaEffect)
  135. {
  136. foreach (AreaEffect areaEffect in AreaEffects)
  137. if (areaEffect != exceptAreaEffect && areaEffect.Spell == spell && IsAffectedByAreaEffect(creature, areaEffect))
  138. return true;
  139. return false;
  140. }
  141. public void BroadcastPacket(IWorldPacket packet)
  142. {
  143. foreach (PlayerCreature pc in Players)
  144. pc.SendPacketToSelf(packet);
  145. }
  146. public void BroadcastPacket(IWorldPacket packet, byte[] rawData)
  147. {
  148. foreach (PlayerCreature pc in Players)
  149. pc.SendPacketToSelf(packet, rawData);
  150. }
  151. }
  152. }