/Addons/WCell.DefaultAddon/Battlegrounds/ArathiBasin/ArathiBase.cs

https://github.com/origins/WCell · C# · 371 lines · 270 code · 57 blank · 44 comment · 29 complexity · 61011557069de701f728f0ceb4cd47e5 MD5 · raw file

  1. using WCell.Addons.Default.Lang;
  2. using WCell.Constants;
  3. using WCell.Constants.Battlegrounds;
  4. using WCell.Constants.World;
  5. using WCell.Core.Timers;
  6. using WCell.RealmServer.Entities;
  7. using WCell.RealmServer.GameObjects.Spawns;
  8. using WCell.RealmServer.Handlers;
  9. using WCell.RealmServer.Lang;
  10. using WCell.Util.Variables;
  11. namespace WCell.Addons.Default.Battlegrounds.ArathiBasin
  12. {
  13. public abstract class ArathiBase
  14. {
  15. /// <summary>
  16. /// Time to convert a cap point in millis
  17. /// </summary>
  18. [Variable("ABCapturePointConversionDelayMillis")]
  19. public static int CapturePointConversionDelayMillis = 60000;
  20. /// <summary>
  21. /// Time until score starts rolling in, after capturing
  22. /// </summary>
  23. [Variable("ABScoreDelayMillis")]
  24. public static int ScoreDelayMillis = 2*1000*60;
  25. #region Events
  26. public event BaseHandler BaseChallenged;
  27. public event BaseHandler CaptureInterrupted;
  28. public event BaseHandler BaseCaptured;
  29. #endregion
  30. #region Fields
  31. private BattlegroundSide _side = BattlegroundSide.End;
  32. private BaseState _state = BaseState.Neutral;
  33. public GameObject FlagStand;
  34. public GameObject ActualAura;
  35. protected GOSpawnEntry neutralBannerSpawn;
  36. protected GOSpawnEntry neutralAuraSpawn;
  37. protected GOSpawnEntry allianceBannerSpawn;
  38. protected GOSpawnEntry allianceAuraSpawn;
  39. protected GOSpawnEntry hordeBannerSpawn;
  40. protected GOSpawnEntry hordeAuraSpawn;
  41. protected GOSpawnEntry allianceAttackBannerSpawn;
  42. protected GOSpawnEntry hordeAttackBannerSpawn;
  43. protected WorldStateId showIconNeutral;
  44. protected WorldStateId showIconAllianceControlled;
  45. protected WorldStateId showIconAllianceContested;
  46. protected WorldStateId showIconHordeControlled;
  47. protected WorldStateId showIconHordeContested;
  48. /// <summary>
  49. /// The character currently capturing the flag.
  50. /// </summary>
  51. public Character Capturer;
  52. public ArathiBasin Instance;
  53. public bool GivesScore;
  54. public TimerEntry StartScoreTimer;
  55. public TimerEntry CaptureTimer;
  56. #endregion
  57. protected ArathiBase(ArathiBasin instance)
  58. {
  59. Instance = instance;
  60. // init timers
  61. CaptureTimer = new TimerEntry(dt =>
  62. {
  63. Capture();
  64. });
  65. StartScoreTimer = new TimerEntry(dt =>
  66. {
  67. GivesScore = true;
  68. });
  69. Instance.RegisterUpdatableLater(StartScoreTimer);
  70. Instance.RegisterUpdatableLater(CaptureTimer);
  71. Names = new string[(int)ClientLocale.End];
  72. AddSpawns();
  73. SpawnNeutral();
  74. }
  75. public abstract string BaseName
  76. {
  77. get;
  78. }
  79. public virtual string[] Names
  80. {
  81. get;
  82. protected set;
  83. }
  84. /// <summary>
  85. /// The side currently in control of this base.
  86. /// If End, base is neutral.
  87. /// </summary>
  88. public BattlegroundSide BaseOwner
  89. {
  90. get { return _side; }
  91. set { _side = value; }
  92. }
  93. /// <summary>
  94. /// The state currently of this base
  95. /// </summary>
  96. public BaseState State
  97. {
  98. get { return _state; }
  99. set { _state = value; }
  100. }
  101. /// <summary>
  102. /// Begins the capturing process. A base will turn if not taken back
  103. /// </summary>
  104. /// <param name="chr"></param>
  105. public void BeginCapture(Character chr)
  106. {
  107. Capturer = chr;
  108. CaptureTimer.Start(CapturePointConversionDelayMillis, 0);
  109. SpawnContested();
  110. ABSounds sound;
  111. if (_side == BattlegroundSide.End)
  112. {
  113. sound = ABSounds.NodeContested;
  114. Instance.WorldStates.SetInt32(showIconNeutral, 0);
  115. }
  116. else if (_side == BattlegroundSide.Alliance)
  117. {
  118. sound = ABSounds.NodeAssaultedHorde;
  119. }
  120. else
  121. {
  122. sound = ABSounds.NodeAssaultedAlliance;
  123. }
  124. if (chr.Battlegrounds.Team.Side == BattlegroundSide.Alliance)
  125. {
  126. State = BaseState.ContestedAlliance;
  127. Instance.WorldStates.SetInt32(showIconAllianceContested, 1);
  128. Instance.WorldStates.SetInt32(showIconHordeControlled, 0);
  129. }
  130. else
  131. {
  132. State = BaseState.ContestedHorde;
  133. Instance.WorldStates.SetInt32(showIconHordeContested, 1);
  134. Instance.WorldStates.SetInt32(showIconAllianceControlled, 0);
  135. }
  136. var time = RealmLocalizer.FormatTimeSecondsMinutes(CapturePointConversionDelayMillis / 1000);
  137. foreach (Character character in Instance.Characters)
  138. {
  139. character.SendSystemMessage(DefaultAddonLocalizer.Instance.Translate(character.Locale, AddonMsgKey.ABBaseClaimed, chr.Name, Names[(int)character.Locale], chr.Battlegrounds.Team.Side, time));
  140. }
  141. MiscHandler.SendPlaySoundToMap(Instance, (uint)sound);
  142. var evt = BaseChallenged;
  143. if (evt != null)
  144. {
  145. evt(chr);
  146. }
  147. }
  148. /// <summary>
  149. /// Call to interrupt the capturing process
  150. /// </summary>
  151. /// <param name="chr">The interrupting character</param>
  152. public void InterruptCapture(Character chr)
  153. {
  154. Capturer = null;
  155. var stats = (ArathiStats)chr.Battlegrounds.Stats;
  156. stats.BasesDefended++;
  157. CaptureTimer.Stop();
  158. if (BaseOwner == BattlegroundSide.Horde)
  159. {
  160. SpawnHorde();
  161. State = BaseState.CapturedHorde;
  162. Instance.WorldStates.SetInt32(showIconHordeControlled, 1);
  163. Instance.WorldStates.SetInt32(showIconAllianceContested, 0);
  164. }
  165. else
  166. {
  167. SpawnAlliance();
  168. State = BaseState.CapturedAlliance;
  169. Instance.WorldStates.SetInt32(showIconAllianceControlled, 1);
  170. Instance.WorldStates.SetInt32(showIconHordeContested, 0);
  171. }
  172. foreach (Character character in Instance.Characters)
  173. {
  174. character.SendSystemMessage(DefaultAddonLocalizer.Instance.Translate(character.Locale, AddonMsgKey.ABBaseDefended, chr.Name, Names[(int)character.Locale]));
  175. }
  176. var evt = CaptureInterrupted;
  177. if (evt != null)
  178. {
  179. evt(chr);
  180. }
  181. }
  182. /// <summary>
  183. /// Call to interrupt the capturing process and begin a new capturing process
  184. /// </summary>
  185. public void ChangeCapture(Character chr)
  186. {
  187. Capturer = chr;
  188. CaptureTimer.Stop();
  189. StartScoreTimer.Stop();
  190. CaptureTimer.Start(CapturePointConversionDelayMillis, 0);
  191. if (chr.Battlegrounds.Team.Side == BattlegroundSide.Alliance)
  192. {
  193. State = BaseState.ContestedAlliance;
  194. Instance.WorldStates.SetInt32(showIconAllianceContested, 1);
  195. Instance.WorldStates.SetInt32(showIconHordeContested, 0);
  196. }
  197. else
  198. {
  199. State = BaseState.ContestedHorde;
  200. Instance.WorldStates.SetInt32(showIconHordeContested, 1);
  201. Instance.WorldStates.SetInt32(showIconAllianceContested, 0);
  202. }
  203. foreach (Character character in Instance.Characters)
  204. {
  205. character.SendSystemMessage(DefaultAddonLocalizer.Instance.Translate(character.Locale, AddonMsgKey.ABBaseAssaulted, chr.Name, Names[(int)character.Locale]));
  206. }
  207. SpawnContested();
  208. }
  209. /// <summary>
  210. /// Finalizes a capture (Flag changes colour (de/respawns, casts spells, etc)
  211. /// </summary>
  212. public void Capture()
  213. {
  214. var stats = (ArathiStats)Capturer.Battlegrounds.Stats;
  215. stats.BasesAssaulted++;
  216. FlagStand.Delete();
  217. ActualAura.Delete();
  218. ABSounds sound;
  219. if (Capturer.Battlegrounds.Team.Side == BattlegroundSide.Horde)
  220. {
  221. BaseOwner = BattlegroundSide.Horde;
  222. State = BaseState.CapturedHorde;
  223. SpawnHorde();
  224. sound = ABSounds.NodeCapturedHorde;
  225. Instance.WorldStates.SetInt32(showIconHordeControlled, 1);
  226. Instance.WorldStates.SetInt32(showIconHordeContested, 0);
  227. }
  228. else
  229. {
  230. BaseOwner = BattlegroundSide.Alliance;
  231. State = BaseState.CapturedAlliance;
  232. SpawnAlliance();
  233. sound = ABSounds.NodeCapturedAlliance;
  234. Instance.WorldStates.SetInt32(showIconAllianceControlled, 1);
  235. Instance.WorldStates.SetInt32(showIconAllianceContested, 0);
  236. }
  237. // It takes a few minutes before a captured flag begins to give score.
  238. StartScoreTimer.Start(ScoreDelayMillis, 0);
  239. foreach (Character character in Instance.Characters)
  240. {
  241. character.SendSystemMessage(DefaultAddonLocalizer.Instance.Translate(character.Locale, AddonMsgKey.ABBaseTaken, BaseOwner, Names[(int)character.Locale]));
  242. }
  243. MiscHandler.SendPlaySoundToMap(Instance, (uint)sound);
  244. var evt = BaseCaptured;
  245. if (evt != null)
  246. {
  247. evt(Capturer);
  248. }
  249. }
  250. public void PlayerClickedOnFlagstand(GameObject go, Character chr)
  251. {
  252. FlagStand.Delete();
  253. ActualAura.Delete();
  254. if (_state == BaseState.Neutral || _state == BaseState.CapturedAlliance || _state == BaseState.CapturedHorde)
  255. {
  256. BeginCapture(chr);
  257. }
  258. else
  259. {
  260. if (_side == BattlegroundSide.End)
  261. ChangeCapture(chr);
  262. else
  263. InterruptCapture(chr);
  264. }
  265. }
  266. protected virtual void AddSpawns()
  267. {
  268. }
  269. /// <summary>
  270. /// Spawn neutral flag (use only at the beginning)
  271. /// </summary>
  272. protected void SpawnNeutral()
  273. {
  274. FlagStand = neutralBannerSpawn.Spawn(Instance);
  275. ActualAura = neutralAuraSpawn.Spawn(Instance);
  276. }
  277. /// <summary>
  278. /// Spawn Horde flag (use only when CaptureTimer = 0)
  279. /// </summary>
  280. protected void SpawnHorde()
  281. {
  282. FlagStand = hordeBannerSpawn.Spawn(Instance);
  283. ActualAura = hordeAuraSpawn.Spawn(Instance);
  284. }
  285. /// <summary>
  286. /// Spawn Alliance flag (use only when CaptureTimer = 0)
  287. /// </summary>
  288. protected void SpawnAlliance()
  289. {
  290. FlagStand = allianceBannerSpawn.Spawn(Instance);
  291. ActualAura = allianceAuraSpawn.Spawn(Instance);
  292. }
  293. /// <summary>
  294. /// Spawn contested flag according to the team which attacks the base
  295. /// </summary>
  296. protected void SpawnContested()
  297. {
  298. if (Capturer.Battlegrounds.Team.Side == BattlegroundSide.Alliance)
  299. FlagStand = allianceAttackBannerSpawn.Spawn(Instance);
  300. else
  301. FlagStand = hordeAttackBannerSpawn.Spawn(Instance);
  302. ActualAura = neutralAuraSpawn.Spawn(Instance);
  303. }
  304. public void Destroy()
  305. {
  306. Capturer = null;
  307. Instance = null;
  308. FlagStand.Delete();
  309. FlagStand.Dispose();
  310. ActualAura.Delete();
  311. ActualAura.Dispose();
  312. }
  313. }
  314. }